1、std::vector::push_back(尾差)
1.1、std::vector::operator[]
意思为;
访问元素
返回对vector容器中位置n的元素的引用。
void test_vector2()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
//下标+[]
for (size_t i = 0; i < v1.size(); i++)
{
v1[i]++;
}
for (size_t i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
}
int main()
{
//test_vector1();
test_vector2();
return 0;
}
2、vector迭代器
vector也是支持迭代器的,其功能和string迭代器是一样的
void test_vector2()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
//下标+[]
for (size_t i = 0; i < v1.size(); i++)
{
v1[i]++;
}
for (size_t i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
//迭代器
vector<int>::iterator it = v1.begin();
while (it != v1.end())
{
(*it)--;
cout << *it <<" ";
it++;
}
cout << endl;
for (auto e : v1)
{
cout << e << " ";
}
}
int main()
{
//test_vector1();
test_vector2();
return 0;
}
2.1、std::find
注意是左闭右开的,如果没有找到会返回last
2.2、std::vector::insert(插入)
vector和string的insert插入的直接区别就是vector的插入坐标位置都是通过迭代器iterator来控制的
void test_vector3()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
for (size_t i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
vector<int>::iterator pos = find(v1.begin(), v1.end(), 3);
if (pos != v1.end())//因为如果查找失败返回的是v1.end()
{
v1.insert(pos, 30);
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
//test_vector1();
test_vector3();
return 0;
}
如果在find查找完后没有进行if判断,这是如果没有找到对应的元素就会变为尾插,但是我们建议还是加上判断条件的
3、std::vector::erase(删除)
void test_vector3()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
for (size_t i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
vector<int>::iterator pos = find(v1.begin(), v1.end(), 3);
if (pos != v1.end())//因为如果查找失败返回的是v1.end()
{
//v1.insert(pos, 30);
v1.erase(pos);
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
//test_vector1();
test_vector3();
return 0;
}
4、std::sort(排序)
使用sort时,需要先包含头文件
#include <algorithm>
排序默认为从小到大排列,如果想从大到小排列需要包含头文件
void test_vector4()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(1);
v1.push_back(32);
v1.push_back(12);
v1.push_back(2);
v1.push_back(4);
v1.push_back(5);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
less<int> ls;
sort(v1.begin(), v1.end(),ls);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
sort(v1.begin(), v1.end());
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
//test_vector1();
test_vector4();
return 0;
}
如果想要从大到小排列,需要包含头文件#include <functional>
void test_vector4()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(1);
v1.push_back(32);
v1.push_back(12);
v1.push_back(2);
v1.push_back(4);
v1.push_back(5);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
less<int> ls;
sort(v1.begin(), v1.end(),ls);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
sort(v1.begin(), v1.end());
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
greater<int> gt;
sort(v1.begin(), v1.end(),gt);
//定义匿名方式
//sort(v1.begin(), v1.end(),greater<int>());
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
//test_vector1();
test_vector4();
return 0;
}
sort不仅可以排序动态容器vector,还可以排序字符串
void test_vector5()
{
string s1("hello1234");
sort(s1.begin(), s1.end(), greater<char>());
cout << s1 << " ";
cout << endl;
}
int main()
{
//test_vector1();
test_vector5();
return 0;
}