Most of the time we initialize an empty instance of STL containers and keep adding elements to or removing elements from it. For example, we create a vector and push_back elements. But what if we want to fill a couple of initial elements?
Sequence container
Use initializer list
Almost all the container types support this kind of initialization.
1
2
| vector<int> a{1,2,3};
deque<int> b{4,5,6};
|
Fill the same number
1
2
3
4
5
| vector<int> a(10);
fill(begin(a), end(a), 0);
deque<int> b(10);
fill(begin(b), end(b), 0);
|
Fill custom numbers
1
2
3
4
5
6
7
8
| vector<int> a(10);
int i = 0;
auto x = [&]() {return i++;};
generate(begin(a), end(a), x);
deque<int> b(10);
generate(begin(b), end(b), x);
|
Fill without predefined size
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| vector<int> a;
fill_n(back_inserter(a), 10, 1);
deque<int> b;
fill_n(back_inserter(b), 10, 1);
vector<int> c;
int i = 0;
auto x = [&]() {return i++;};
int n = 10;
generate_n(back_inserter(c), n, x);
deque<int> d;
generate_n(back_inserter(d), n, x);
|
Associative container
set, unordered_setmap, unordered_map
Initializer list
1
2
| set<int> a{1,2,3};
map<int, int> b{ {1,2}, {2,3} };
|
Fill different number
1
2
3
4
5
6
7
| set<int> a;
map<int, int> b;
int i = 0;
int n = 10;
generate_n(inserter(a, a.end()), n, [&]() {return i++;})
generate_n(inserter(b, b.end()), n, [&](){return make_pair(i++, 0);});
|
The iterators dereferenced from set or map is const which means we cannot directly modify it.
1
2
| set<int> a{3};
*a.begin() = 3;
|
Due to previous reasons, we cannot use fill or fill_n to fill set or map.
Other possible methods
swap: swap two containersassign: clean the elements and replace them- constructors
1
2
3
4
5
6
7
8
9
| vector<int> a{1,2,3};
vector<int> b;
b.swap(a);
vector<int> c;
c.assign(b.begin(), b.end());
vector<int> d(c);
vector<int> e(c.begin(), c.end());
|