1、容器:序列容器(时间决定)、关联式容器(容器中的数据有一定规则)

2、迭代器:通过迭代器寻找、遍历容器中的数据

vetor的使用:数据遍历与输出

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector> //向量 动态数组
#include<algorithm> //算法头文件
#include<string>
using namespace std; void myPrint(int val){
cout << val << " ";
} //1. STL中的容器算法迭代器
void test01(){ //容器
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50); //获得开始迭代器
vector<int>::iterator begin = v.begin();
//获得结束位置迭代器
vector<int>::iterator end = v.end(); //遍历算法
for_each(begin, end, myPrint);
cout << endl;
} //2. 容器可以存放对象
class Person{
friend ostream& operator<<(ostream& out, Person &person);
public:
Person(string name,int age){
this->mName = name;
this->mAge = age;
}
public:
string mName;
int mAge;
}; ostream& operator<<(ostream& out, Person &person){
out << "Name:" << person.mName << " Age:" << person.mAge << endl;
return out;
}
void test02(){ vector<Person> v;
//在向容器中插入元素的时候,一定要保证元素能够被拷贝。
v.push_back(Person("aaa", 10));
v.push_back(Person("bbb", 20));
v.push_back(Person("ccc", 30));
v.push_back(Person("ddd", 40));
v.push_back(Person("eee", 50)); vector<Person>::iterator begin = v.begin();
vector<Person>::iterator end = v.end(); while (begin != end){
cout << (*begin);
++begin;
}
} //3. 存放对象指针
void test03(){ vector<Person *> v; //创建数据
Person *p1 = new Person("aaa", 10);
Person *p2 = new Person("bbb", 20);
Person *p3 = new Person("ccc", 30);
Person *p4 = new Person("ddd", 40);
Person *p5 = new Person("eee", 50); v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5); vector<Person *>::iterator begin = v.begin();
vector<Person *>::iterator end = v.end(); while (begin != end){
cout << (*begin)->mName << " " << (*begin)->mAge << endl;
++begin;
} delete p5;
delete p4;
delete p3;
delete p2;
delete p1; } //4. 容器可以嵌套容器
void test04(){ vector<vector<int>> vs; vector<int> v1;
vector<int> v2;
vector<int> v3;
vector<int> v4;
vector<int> v5; for (int i = 0; i < 5;i ++){
v1.push_back(i + 10);
v2.push_back(i + 20);
v3.push_back(i + 30);
v4.push_back(i + 40);
v5.push_back(i + 50);
} vs.push_back(v1);
vs.push_back(v2);
vs.push_back(v3);
vs.push_back(v4);
vs.push_back(v5); vector<vector<int>>::iterator begin = vs.begin();
vector<vector<int>>::iterator end = vs.end(); while (begin != end){ vector<int>::iterator sbegin = (*begin).begin();
vector<int>::iterator send = (*begin).end(); while (sbegin != send){
cout << *sbegin << " ";
++sbegin;
}
cout << endl; ++begin;
} cout << "-------------------" << endl; for (vector<vector<int>>::iterator it = vs.begin(); it != vs.end(); ++it){
for (vector<int>::iterator sit = it->begin(); sit != it->end(); ++sit){
cout << *sit << " ";
}
cout << endl;
} } int main(){ //test01();
//test02();
//test03();
test04(); system("pause");
return EXIT_SUCCESS;
}

Vector常用API

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std; void printVector(const vector<int> &vec){
for (vector<int>::const_iterator it = vec.begin(); it != vec.end(); ++it){
cout << *it << " ";
}
cout << endl;
} void printReverseVector(vector<int> &vec){
for (vector<int>::reverse_iterator it = vec.rbegin(); it != vec.rend(); ++it){
cout << *it << " ";
}
cout << endl;
} //1. vector构造
/*
vector<T> v; //采用模板实现类实现,默认构造函数
vector(v.begin(), v.end());//将v[begin(), end())区间中的元素拷贝给本身。
vector(n, elem);//构造函数将n个elem拷贝给本身。
vector(const vector &vec);//拷贝构造函数。 //例子 使用第二个构造函数 我们可以...
int arr[] = {2,3,4,1,9};
vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));
*/
void test01(){ int arr[] = { 2, 3, 4, 1, 9 };
vector<int> v(arr,arr+ sizeof(arr)/sizeof(int)); printVector(v);
printReverseVector(v); vector<int> v2(10, 6);
printVector(v2);
} //2. vector常用赋值操作
/*
assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。
vector& operator=(const vector &vec);//重载等号操作符
swap(vec);// 将vec与本身的元素互换。
*/
void test02(){
vector<int> v;
v.assign(10, 6); vector<int> v2;
v2.push_back(1);
v2.push_back(2);
v2.push_back(3); printVector(v);
printVector(v2); cout << "-----------------" << endl;
v.swap(v2);
printVector(v);
printVector(v2); //v.assign(v2.begin(),v2.end());
//printVector(v);
} //3. 大小操作
/*
size();//返回容器中元素的个数
empty();//判断容器是否为空
resize(int num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem);//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长>度的元素被删除。
capacity();//容器的容量
reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。
*/ void test03(){ //1. resize开辟空间,并且初始化
//2. reserve只开辟空间,没有初始化
vector<int> v2;
v2.push_back(1);
v2.push_back(2);
v2.push_back(3); cout << "size:" << v2.size() << endl;
v2.resize(5); //改变容器中元素的个数,多余的扔掉
cout << "size:" << v2.size() << endl;
printVector(v2); v2.reserve(20); //预留空间
v2.push_back(10);
printVector(v2);
cout << "size:" << v2.size() << endl;
} void test04(){ vector<int> v;
v.resize(5);
v.push_back(10);
cout << "capacity:" << v.capacity() << endl;
cout << "size:" << v.size() << endl; cout << "----------" << endl;
vector<int> v2;
v2.reserve(5);
v2.push_back(10);
cout << "capacity:" << v2.capacity() << endl;
cout << "size:" << v2.size() << endl; //cout << v2[2] << endl;
} void test05(){ vector<int> v; v.reserve(100000); int *p = NULL;
int count = 0;
for (int i = 0; i < 100000;i ++){
v.push_back(i);
if (p != &v[0]){
p = &v[0];
count++;
}
}
cout << count << endl;
} //swap用法
void test06(){ vector<int> v;
for (int i = 0; i < 100000; i++){
v.push_back(i);
} cout << "容量:" << v.capacity() << endl;
cout << "大小:" << v.size() << endl; cout << "---------------" << endl;
v.resize(10);
cout << "容量:" << v.capacity() << endl;
cout << "大小:" << v.size() << endl; cout << "---------------" << endl;
vector<int>(v).swap(v);
cout << "容量:" << v.capacity() << endl;
cout << "大小:" << v.size() << endl;
} //7. vector数据存取操作
/*
at(int idx); //返回索引idx所指的数据,如果idx越界,抛出out_of_range异常。
operator[];//返回索引idx所指的数据,越界时,运行直接报错
front();//返回容器中第一个数据元素
back();//返回容器中最后一个数据元素
*/
void test07(){ vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3); cout << v.front() << endl;
cout << v.back() << endl; v.front() = 100;
v.back() = 200; for (int i = 0; i < v.size(); i ++){
cout << v[i] << " ";
}
cout << endl;
} //8. vector插入删除
/*
insert(const_iterator pos, int count,ele);//迭代器指向位置pos插入count个元素ele.
push_back(ele); //尾部插入元素ele
pop_back();//删除最后一个元素
erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素
erase(const_iterator pos);//删除迭代器指向的元素
clear();//删除容器中所有元素
*/
void test08(){ vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40); v.insert(v.begin()+2,100);
printVector(v); v.pop_back();
printVector(v);
v.erase(v.begin());
printVector(v);
v.erase(v.begin(),v.end());
cout << "size:" << v.size() << endl; } int main(){ //test01();
//test02();
//test03();
//test04();
//test05();
//test06();
//test07();
test08(); system("pause");
return EXIT_SUCCESS;
}

最新文章

  1. API Monitor简介(API监控工具)
  2. SQL经典面试题及答案
  3. Hadoop:输入,输出,key,value格式
  4. C#泛型-小心使用静态成员变量
  5. MySQL表复制
  6. K. Perpetuum Mobile
  7. 从覆盖bootstrap样式谈css选择器优先级
  8. CMT2300 收发一体 SUB 1G 支持灵活选频
  9. linux下^M问题
  10. luogu4365 秘密袭击 (生成函数+线段树合并+拉格朗日插值)
  11. 47)django-以put和delete方式提交数据
  12. 有标号的DAG计数(FFT)
  13. 论证与测试 + 用EA画uml
  14. 使用PHPMail发送邮箱(163邮箱为例)
  15. mac 关闭显示器 &amp; 快捷键
  16. Lua脚本语言入门学习其应用教程
  17. 【转载】HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
  18. 2018 Multi-University Training Contest 9 Solution
  19. Window 平台安装 Python:
  20. Java Web 远程调试

热门文章

  1. python-绘图与可视化
  2. MatrixOne Linux 编译文档
  3. Kafka之工作流程分析
  4. 通过openlayers加载dwg格式的CAD图并与互联网地图叠加
  5. 你的哪些骚操作会导致Segmentation Fault&#128514;
  6. [Err] 1052 - Column ‘roleId‘ in where clause is ambiguous
  7. vue中动态引入图片为什么要是require, 你不知道的那些事
  8. scrapy传递 item时的 数据不匹配 和一些注意事项
  9. pycharm系列---django
  10. 源码级深度理解 Java SPI