参考:https://blog.csdn.net/qq_32867925/article/details/104851126
https://blog.csdn.net/dongyanxia1000/article/details/52838922
按顺序存入容器用push_back();
取最后一个元素用pop_back();
获取最后一个元素back
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
vector<int> vec;
int sum = 0, i = 0;
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
cout << "init vec.size() = "<< vec.size() << endl;
while(!vec.empty())
{
i++;
sum += vec.back(); //取最后一个元素
vec.pop_back(); //删除最后一个元素
printf("after %d time, sum: %d size = %d\n", i, sum, vec.size());
}
system("pause");
return 0;
}
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
vector<int> vec;
for( int i = 0; i < 10; i++ )
{
vec.push_back( i );
}
printf("init vec size: %d\n", vec.size()); //0-9
vec.erase( vec.begin() + 5 ); //删除第6个元素,{0 1 2 3 4 6 7 8 9}
printf("after erase size: %d\n", vec.size());//9
vec.erase( vec.begin(), vec.begin() + 3 ); //删除第1个元素,到第4个元素 {3 4 6 7 8 9}
printf("vec = { ");
for( unsigned int j = 0; j < vec.size(); j++ )
{
printf("%d ", vec[j]);
}
printf("}\n");
system("pause");
return 0;
}
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
vector<int> vec;
vec.push_back(100);
vec.push_back(300);
vec.push_back(300);
vec.push_back(300);
vec.push_back(300);
vec.push_back(500);
cout << &vec << endl;
vector<int>::iterator itor;
for( itor = vec.begin(); itor != vec.end(); itor++ )
{
if( *itor == 300 )
{
itor = vec.erase(itor);
}
}
for( itor = vec.begin(); itor != vec.end(); itor++)
{
cout<< *itor <<" ";
}
system("pause");
return 0;
}
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <stdio.h>
using namespace std;
void Show(int v)
{
std::cout << v << ' ';
}
int main()
{
int age[10] ={ 2, 3, 5, 4, 2, 3, 2, 6, 2, 7 };
std::list<int> list_a(age, age + 10);
std::list<int> list_b(list_a);
printf("Original list_a:\n");
for_each(list_a.begin(), list_a.end(), Show);
printf("\n\n");
list_a.remove( 2 );
printf("remove() method list_a:\n");
for_each(list_a.begin(), list_a.end(), Show);
printf("\n\n");
list<int>::iterator itor;
itor = remove(list_b.begin(), list_b.end(), 2);
printf("remove() function list_b:\n");
for_each(list_b.begin(), list_b.end(), Show);
printf("\n\n");
list_b.erase( itor, list_b.end() );
printf("erase() method list_b:\n");
for_each(list_b.begin(), list_b.end(), Show);
printf("\n");
system("pause");
return 0;
}
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <stdio.h>
using namespace std;
void Show(int v)
{
std::cout << v << ' ';
}
int main()
{
int age[10] ={ 2, 3, 5, 4, 2, 3, 2, 6, 2, 7 };
vector<int> vec_a(age, age + 10);
printf("Original vec_a:\n");
for_each(vec_a.begin(), vec_a.end(), Show);
printf("\n\n");
vector<int>::iterator itor;
itor = remove(vec_a.begin(), vec_a.end(), 2);
printf("remove() function vec_a:\n");
for_each(vec_a.begin(), vec_a.end(), Show);
printf("\n");
printf("remove() size: %d\n\n", vec_a.size());
vec_a.erase( itor, vec_a.end() );
printf("erase() method vec_a:\n");
for_each(vec_a.begin(), vec_a.end(), Show);
printf("\n");
printf("erase() size: %d\n\n", vec_a.size());
system("pause");
return 0;
}