stl中的另一个容器vector
#include <iostream>
#include <vector>
#include <string>
using std::vector;
int main()
{
vector<std::string> strings(10, "I Love Mark!");
vector<std::string> otherstrings(2, "Yoyoyo");
vector<int> ints(10, 100);
strings.push_back("I So Love Mark!!!"); // 在末尾添加
strings.push_back("I So Love Mark!!!"); // 在末尾添加
strings.assign(10, "I Love Mark!!!!!!");
//strings = otherstrings; // vector容器级别的交换 vector.operator=
// 等价于
// strings.opertor=(otherstrings)
strings[1] = otherstrings[1]; // vector容器中的元素级别的交换 string.operator=
// 等价于
// (string &(strings.operator[](1))).operator=(string &(otherstrings.operator[](1)))
strings.at(0) = "1000";
// 等价于
// (string &(string.at(0))).operator=(char *"1000")
strings.back() = "666666";
for (std::size_t i = 0; i < strings.size(); ++i)
{
std::cout << strings.at(i) << std::endl;
}
return 0;
}
vector中的迭代器
![这里写图片描述](https://img-blog.csdn.net/20170115125537598? watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTM3MTYyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
#include <iostream>
#include <vector>
#include <string>
using std::vector;
int main()
{
vector<std::string> strings(10, "66666");
vector<std::string>::iterator it = strings.begin();
std::cout << *it << std::endl;
std::cout << it->c_str() << std::endl;
//for (vector<std::string>::iterator it = strings.begin(); it != strings.end(); ++it)
//for (auto it = strings.begin(); it != strings.end(); ++it) // auto是c++11中新加的功能
for (auto it = strings.begin(); it < strings.end(); ++it) // 也可以使用小于号
{
std::cout << *it << std::endl;
}
return 0;
}
erase函数