STL
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void myprint(int val) {
cout << val << endl;
}
void test01(){
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
vector<int>::iterator itbegin = v.begin();
vector<int>::iterator itend = v.end();
while (itbegin != itend) {
cout << *itbegin << endl;
itbegin++;
}
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << endl;
}
for_each(v.begin(), v.end(), myprint);
}
int main() {
test01();
system("pause");
return 0;
}
vector构造函数
#include<iostream>
#include<vector>
using namespace std;
void printVector(vector<int> &v) {
for (vector<int> ::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << endl;
}
}
void test01() {
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
printVector(v1);
vector<int> v2(v1.begin(), v1.end());
printVector(v2);
vector<int> v3(10, 100);
vector<int> v4(v3);
printVector(v4);
}
int main() {
test01();
return 0;
}
vector插入和删除
#include<iostream>
#include<vector>
using namespace std;
void printvector(vector<int>& v) {
for (vector<int> ::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
}
void test01() {
vector<int> v1;
v1.push_back(15);
v1.push_back(16);
v1.pop_back();
v1.insert(v1.begin(), 100);
v1.insert(v1.begin(), 2, 100);
v1.erase(v1.begin());
v1.erase(v1.begin(), v1.end());
v1.clear();
}
int main() {
test01();
return 0;
}
vector中存放自定义的数据类型
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
class person {
public:
person(string name, int age) {
this->m_age = age;
this->m_name = name;
}
string m_name;
int m_age;
};
void test01() {
vector<person>v;
person p1("a", 10);
person p2("b", 20);
person p3("c", 30);
person p4("f", 40);
person p5("s", 50);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
for (vector<person>::iterator it = v.begin(); it != v.end(); it++) {
cout << "姓名:" << (*it).m_name << "年龄:" <<it->m_age << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
vector的赋值操作
#include<iostream>
#include<vector>
using namespace std;
void printvector(vector<int>& v) {
for (vector<int> ::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
}
void test01() {
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
printvector(v1);
vector<int> v2;
v2 = v1;
printvector(v2);
vector<int> v3;
v3.assign(v1.begin(), v1.end());
printvector(v3);
vector<int> v4;
v4.assign(10, 100);
}
int main() {
test01();
return 0;
}
vector互换容器
#include<iostream>
#include<vector>
using namespace std;
void test01() {
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
vector<int> v2;
for (int i = 10; i > 0; i++) {
v2.push_back(i);
}
v1.swap(v2);
vector<int> v(1000000, 1);
v.resize(3);
vector<int>(v).swap(v);
}
int main() {
test01();
return 0;
}
vector中容器的大小和容量
#include<iostream>
#include<vector>
using namespace std;
void printvector(vector<int>& v) {
for (vector<int> ::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
}
void test01() {
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
printvector(v1);
if (v1.empty()) {
cout << "v1为空";
}
else {
cout << "容器的容量为:" << v1.capacity();
cout << "容器的大小为:" << v1.size();
}
v1.resize(15);
v1.resize(17, 100);
v1.resize(5);
}
int main() {
test01();
return 0;
}
vector容器嵌套容器
#include<iostream>
#include<vector>
using namespace std;
void test01() {
vector<vector<int>>v;
vector<int> v1;
vector<int> v2;
vector<int> v3;
vector<int> v4;
for (int i = 0; i < 4; i++) {
v1.push_back(i + 1);
v2.push_back(i + 2);
v3.push_back(i + 3);
v4.push_back(i + 4);
}
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
for (vector<vector<int>> ::iterator it = v.begin(); it != v.end(); it++) {
for (vector<int>::iterator i = (*it).begin(); i != (*it).end(); i++) {
cout << (*i) << " ";
}
cout << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
vector数据存取
#include<iostream>
#include<vector>
using namespace std;
void test02() {
vector<int> v1;
for (int i = 0; i < v1.size(); i++) {
cout << v1[i] << endl;
}
for (int i = 0; i < v1.size(); i++) {
cout << v1.at(i) << " ";
}
}
int main() {
test02();
return 0;
}