vector容器
#include <iostream>
#include <vector>
#include <algorithm>
template <typename T>
void display(const std::vector<T> &arr){
for (const auto &i: arr)
std::cout << i << " ";
std::cout << std::endl;
}
void test1()
{
std::vector <int> v1 {1, 2, 3, 4, 5};
display(v1);
v1 = {2, 2, 2};
display(v1);
std::vector <int> v2(10, 88);
display(v2);
}
void test2()
{
std::vector <int> v1 {1, 2, 3, 4, 5};
display(v1);
std::cout << "size: " << v1.size() << std::endl;
std::cout << "capacity: " << v1.capacity() << std::endl;
std::cout << "max_size: " << v1.max_size() << std::endl;
v1.push_back(6);
display(v1);
std::cout << "size: " << v1.size() << std::endl;
std::cout << "capacity: " << v1.capacity() << std::endl;
std::cout << "max_size: " << v1.max_size() << std::endl;
v1.shrink_to_fit();
std::cout << "size: " << v1.size() << std::endl;
std::cout << "capacity: " << v1.capacity() << std::endl;
std::cout << "max_size: " << v1.max_size() << std::endl;
v1.resize(100);
std::cout << "size: " << v1.size() << std::endl;
std::cout << "capacity: " << v1.capacity() << std::endl;
std::cout << "max_size: " << v1.max_size() << std::endl;
}
void test3()
{
std::vector <int> v1 {1, 2, 3, 4, 5};
display(v1);
v1[0] = 100;
v1.at(1) = 200;
display(v1);
std::cout << "获取第一个元素: " << v1.front() << std::endl;
std::cout << "获取最后一个元素: " << v1.back() << std::endl;
v1.pop_back();
display(v1);
}
void test4()
{
std::vector <int> v1 {1, 2, 3, 4, 5};
display(v1);
v1.clear();
display(v1);
v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
display(v1);
v1.erase(v1.begin(), v1.begin() + 3);
display(v1);
v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
display(v1);
std::vector <int>::iterator it = v1.begin();
while (it != v1.end())
{
if (*it % 2 == 0)
v1.erase(it);
else
it++;
}
display(v1);
}
void test5()
{
std::vector <int> v1 {1, 2, 3, 4, 5};
std::vector <int> v2 {10, 20, 30, 40, 50};
v1.swap(v2);
display(v1);
display(v2);
}
void test6()
{
std::vector <int> v1 {2, 1, 4, 5, 3};
std::sort(v1.begin(), v1.end());
display(v1);
}
void test7()
{
std::vector <int> v1 {1, 2, 3, 4, 5};
std::vector <int> v2 {10, 20, 30, 40, 50};
std::copy(v1.begin(), v1.end(), std::back_inserter(v2));
v1 = {1, 2, 3};
v2 = {10, 20};
std::copy_if(v1.begin(), v1.end(), std::back_inserter(v2),
[](int x){ return x % 2 ==0;});
display(v1);
display(v2);
}
void test8()
{
std::vector<int> v1{1, 2, 3, 4, 5};
std::vector<int> v2{10, 20, 30, 40, 50};
std::vector<int> v3;
std::transform(v1.begin(), v1.end(), v2.begin(), std::back_inserter(v3),
[](int x, int y)
{ return x + y; });
std::cout << "v1 + v2 = " << std::endl;
display(v3);
v3.clear();
std::transform(v1.begin(), v1.end(), v2.begin(), std::back_inserter(v3),
[](int x, int y)
{ return x * y; });
std::cout << "v1 * v2 = " << std::endl;
display(v3);
}
void test9()
{
std::vector<int> v1{1, 2, 3, 4, 5};
std::vector<int> v2{10, 20, 30, 40, 50};
auto it = std::find(v1.begin(), v1.end(), 5);
if (it != v1.end())
{
std::cout << "已找到" << std::endl;
v1.insert(it, v2.begin(), v2.end());
display(v1);
}
else
{
std::cout << "未找到" << std::endl;
}
}
int main()
{
test9();
}