-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorInsertTest.cpp
More file actions
49 lines (41 loc) · 1.08 KB
/
VectorInsertTest.cpp
File metadata and controls
49 lines (41 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>
#include <assert.h>
std::vector<int>::iterator insert(std::vector<int>& v, size_t pos, const int& val) {
size_t size= v.size();
size_t offset = size - pos;
assert(pos >= 0 && pos <= size);
std::vector<int>::iterator it = v.end();
std::vector<int>::iterator it1 = it - 1;
if ( offset == 0 ) {
v.push_back(val);
return it;
}
v.push_back(*it1);
for (int i = offset; i > 1; i--) {
it1 -= 1;
it -= 1;
*it = *it1;
}
*it1 = val;
return it1;
}
int main() {
std::vector<int> v;
std::vector<int>::iterator it;
int elem = 777;
size_t position = 0;
for (int i = 1; i < 10; i++) {
v.push_back(i);
}
it = insert(v, position, elem);
std::cout << "\nInserted element: " <<*it;
std::cout << " at position: " << position << " \n";
it = v.begin();
std::cout << "Vector: " << "[ " << *it;
for (it=v.begin() + 1; it!= v.end(); it++) {
std::cout << " " << *it;
}
std::cout << " ]" << std::endl;
return 0;
}