Library to enhance C++ built-in vector class

This library contains various utility functions/classes to support std::vector usage. In particular, the class called “stdvec_wrap” wraps (without copying) std::vector and allows slicing (either continuous range, or indices), accessing elements, deleting multiple elements with indices, overloaded operators ==, >, etc. for finding and indexing, etc.


/*
Demo:

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10,11,12,13 };
cout << stdvec_wrap<int>(v) << endl;

Output:

1 2 3 4 5 6 7 8 9 10
11 12 13

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10,11,12,13 };
cout << stdvec_wrap<int>(v, 3, -1) << endl;

Output:

4 5 6 7 8 9 10 11 12 13

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10,11,12,13 };
stdvec_wrap<int> a(v, 3, -1);
cout << a << endl;

Output:

4 5 6 7 8 9 10 11 12 13

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v, 3, -1);
a[0] = 99;
cout << v << endl;

Output:

1 2 3 99 5 6 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
a[0] = 99;
cout << v << endl;

Output:

99 2 3 4 5 6 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int>(v, 3, -1)[0] = 999;
cout << v << endl;

Output:
1 2 3 999 5 6 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
std::vector<int> v2 = { 11,12,13,14,15,16,17 };
stdvec_wrap<int> a(v, 3, -1);
a = v2;
cout << v << endl;

Output:

1 2 3 11 12 13 14 15 16 17

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
std::vector<int> v2 = { 11,12,13,14,15,16,17 };
stdvec_wrap<int>(v, 3, -1) = v2;
cout << v << endl;

Output:

1 2 3 11 12 13 14 15 16 17

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
std::vector<int> v2 = stdvec_wrap<int>(v, 3, -1).deep();
cout << v2 << endl;

Output:

4 5 6 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
std::vector<int> v2 = stdvec_wrap<int>(v, {0, 3, 8}).deep();
cout << v2 << endl;

Output:

1 4 9

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
cout << stdvec_wrap<int>(v, { 0, 3, 8 }) << endl;

Output:

1 4 9

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int>(v, { 0, 3, 8 }) = {99,100,101};
cout << v << endl;

Output:

99 2 3 100 5 6 7 8 101 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int>(v, { 0, 3, 8 }) = std::vector<int>({ 99,100,101 });
cout << v << endl;

Output:

99 2 3 100 5 6 7 8 101 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int>(v, { 0, 3, 8 }) = stdvec_wrap<int>(v, { 5, 9, 2 });
cout << v << endl;

Output:

6 2 3 10 5 6 7 8 3 10

Note: NO copying was ever made for the entire vectors. Just replace the elements
at its own positions.

Code:

std::vector<int> v = { 3,2,3,4,5,6,7,8,9,3};
stdvec_wrap<int> a(v);
// find indices where “3” occurs
cout << (a == 3) << endl;

Output:

0 2 9

Code:

// replace all values “3” in v with 100
std::vector<int> v = { 3,2,3,4,5,6,7,8,9,3};
stdvec_wrap<int> a(v);
stdvec_wrap<int>(v, a == 3) = 100;
cout << v << endl;

Output:

100 2 100 4 5 6 7 8 9 100

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
stdvec_wrap<int>(v, a == 3) = 100;
cout << v << endl;

Output:

1 2 100 4 5 6 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
stdvec_wrap<int>(v, a <= 3) = 100;
cout << v << endl;

Output:

100 100 100 4 5 6 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
stdvec_wrap<int>(v, a > 8) = 45;
cout << v << endl;

Output:
1 2 3 4 5 6 7 8 45 45

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
// show vector of indices
cout << (a > 8) << endl;

Output:

8 9

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
std::vector<int> pp = a > 8;
cout << pp << endl;

Output:

8 9

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
stdvec_wrap<int>(v, a <= 4) = 45;
cout << v << endl;

Output:

45 45 45 45 5 6 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v, 2, -1);
std::vector<int> v2 = a.deep();
stdvec_wrap<int>(v2, a <= 4) = 45;
cout << v2 << endl;

Output:

45 45 5 6 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
// delete 3rd element
a.del(2);
// original v
cout << v << endl;
// the wrapper object
cout << a << endl;
// it shouldn’t affect v as it’s just O(1) operation
cout << v << endl;
// only when use deep, it will produce a new std::vector
// with all the latest changes.
v = a.deep();
cout << v << endl;

Output:

1 2 3 4 5 6 7 8 9 10

1 2 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

1 2 4 5 6 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
a.del({ 2,4,5 });
std::vector<int> v2 = a.deep();
cout << v2 << endl;

Output:

1 2 4 7 8 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
cout << v << endl;
a.del(2);
cout << a.deep() << endl;
a.del(4);
cout << a.deep() << endl;
a.del(5);
cout << a.deep() << endl;

Output:

1 2 3 4 5 6 7 8 9 10

1 2 4 5 6 7 8 9 10

1 2 4 5 7 8 9 10

1 2 4 5 7 9 10

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10};
stdvec_wrap<int> a(v);
cout << v << endl;
a.del({0,2,4});
cout << a.deep() << endl;
a.del(0);
cout << a.deep() << endl;
a.del({ 0,1 });
cout << a.deep() << endl;
a.del(0);
cout << a.deep() << endl;

Output:

1 2 3 4 5 6 7 8 9 10

2 4 6 8 9 10

4 6 8 9 10

8 9 10

9 10

Code:

// due to the use of reference instead of copying, it is possible
// to “chain” operations without ever making a copy
std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10 };
// wrap a slice
stdvec_wrap<int> w(v, 4, -1);
// find indices where number is less than 7
cout << (w < 7) << endl;
// wrap the whole vector
stdvec_wrap<int> w2(v);
// find indices where number is less than 7
cout << (w2 < 7) << endl;

Output:

0 1

0 1 2 3 4 5

Code:

stdvec_wrap<int> w(v);
w.sample(4);
cout << w.deep() << endl;

Output:

5 10 1 7

Code:

stdvec_wrap<int> w(v);
w.sample(40);
cout << w.deep() << endl;

Output:

4 8 10 5 2 2 10 1 7 9
10 8 9 7 7 2 9 7 8 5
2 10 10 7 8 10 6 9 7 9
10 1 7 9 7 8 2 5 10 7

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10 };
stdvec_wrap<int> w(v);
w.sample(10);
cout << w.deep() << endl;

Output:

4 5 4 1 4 9 2 5 10 3

Code:

std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10 };
stdvec_wrap<int> w(v);
w.sample_no_replace(10);
cout << w.deep() << endl;

Output:

9 2 10 3 1 6 8 4 5 7


The code is available at: https://github.com/Kyaw-Kyaw-Htike/Library-to-enhance-Cpp-built-in-vector-class

  • Programming languages: C++