-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.cpp
115 lines (90 loc) · 3.61 KB
/
vector.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
void print(std::vector<std::string> list){
std::cout << "Vector Size : " << list.size() << std::endl ;
std::cout << "{ " ;
for (int i=0 ;i<list.size() ;i++){
if (i<list.size()-1){
std::cout << list[i] << " ,";
}
else{
std::cout << list[i] ;
}
}
std::cout << " }" ;
std::cout << "\n" ;
}
int main(){
std::vector<std::string> strVector;
strVector.push_back("hello1") ; //append value
// {"hello1"}
strVector.push_back("hello2") ;
// {"hello1" ,"hello2"}
strVector.pop_back() ; //remove last element
// {"hello1"}
strVector.assign(6,"hello3") ; //reset vector & assign new value 6 times
// {"hello3" ,"hello3" ,"hello3" ,"hello3" ,"hello3" ,"hello3" }
strVector.assign(0,"") ; //reset vetor
// { }
strVector.push_back("hello1") ;
strVector.push_back("hello2") ;
strVector.push_back("hello3") ;
// {"hello1" ,"hello2" ."hello3" }
strVector.at(2) = "hello5" ; // change index 2 element -> strVector[2] = "hello5"
// {"hello1" ,"hello2" ."hello5" }
strVector.back() = "hello3" ; //change last value in vector
// {"hello1" ,"hello2" ."hello3" }
strVector.front() = "hello5" ; //change first value in vector
// {"hello5" ,"hello2" ."hello3" }
strVector.front() = "hello1" ;
// {"hello1" ,"hello2" ."hello3" }
strVector.clear() ; //delete all element in vector
// {}
strVector.push_back("hello1") ;
strVector.push_back("hello2") ;
strVector.push_back("hello3") ;
// {"hello1" ,"hello2" ,"hello3"}
strVector.erase(strVector.begin(),strVector.end()) ;
// {}
strVector.push_back("hello1") ;
strVector.push_back("hello2") ;
strVector.push_back("hello3") ;
// {"hello1" ,"hello2" ,"hello3"}
strVector.erase(strVector.begin()+1,strVector.end()-1) ; //ERASE elemnet to specific index
// {"hello1","hello3"}
strVector.insert(strVector.begin()+1,"hello2") ;
// {"hello1","hello2","hello3"}
strVector.resize(2) ; //resize vector size
// {"hello1","hello2"}
std::vector<std::string> strVector2;
strVector2.assign(3,"hello3");
strVector.swap(strVector2) ; //swap with another vector
// {"hello3","hello3","hello3"}
strVector.emplace(strVector.begin() ,"zero") ; // add to index 0 //append item to specific index
// {"zero","hello3","hello3","hello3"}
strVector.emplace(strVector.end() ,"last") ;
// {"zero","hello3","hello3","hello3","last" }
strVector.emplace(strVector.end()-1 ,"last2") ;
// {"zero","hello3","hello3","hello3" ,"last2" ,"last" }
std::cout << strVector.size() ; //get no. of elements in vector
std::cout << strVector.max_size() ;//get maximum size of vector
std::cout << strVector.empty() ; //check vector is empty true or false
std::cout << strVector.capacity() ; //get no. of elements allocated by vector
strVector.reserve(1000) ; // element count set to vector
strVector.begin() ; //begin pointer of vector
strVector.end() ; //end pointer of vector
std::cout << strVector.at(0) ; //get 2 index element
std::cout << strVector.back() ; // get last element
std::cout << strVector.front() ; //get first element
//strVector.rbegin() ;
// strVector.get_allocator() ;
//strVector.shrink_to_fit() ;
//strVector.emplace() ;
//strVector.cbegin() ;
//strVector.cend() ;
//strVector.data() ;
//strVector.emplace_back() ;
//strVector.resize() ;
print(strVector) ;
std::vector<std::vector<std::vector<int>>> s = {{{76},{89}} ,{{78},{89}},{{87},{89}}} ;
}