Vector比数组好用多了,毕竟是可变长的.
常用来建图.
需要掌握的就是构造函数,添加数据,删除数据.
#include<set>
#include<ctime>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 1100000
using namespace std;
int main(){
srand(time(NULL));
vector<int> v1; ///empty
vector<int> v2(5);///5 elements
vector<int> v3(5,1);///5 elements all is 1
vector<vector<int> >v4;/// two axis with empty
vector<vector<int> >v5[5]; /// int a[5][] col is empty
vector<vector<int> >v6(8,vector<int>(5,0)); /// int a[8][5] all is 0
/**==================================================================**/
printf("v1‘s size is %d\n",v1.size());
for(int i = 0;i<10;i++)
v1.push_back(rand()),printf("%d ",*v1.rbegin());puts("");///insert
printf("v1‘s size is %d\n",v1.size());
for(int i = 0;i<10;i++)
printf("%d ",v1[i]);puts("");///visit
sort(v1.begin(),v1.end(),less<int>());
printf("having been sorted:\n");
for(int i = 0;i<10;i++)
printf("%d ",v1[i]);puts("");
while(v1.size())
v1.erase(v1.begin());
printf("\nv1‘s size is %d\n",v1.size());///erase
for(int i = 0;i<5;i++){
v4.push_back(vector<int>(5));
for(int j = 0;j<5;j++)
v4[i].push_back(rand());
}
return 0;
}
void test_vector_constructor()
{
// 0. Create an empty vector v0
std::vector<int> v0;
assert(v0.empty());
// 1. Create a vector v1 with 3 elements of default value 0
std::vector<int> v1(3);
// 2. Create a vector v2 with 5 elements of value 2
std::vector<int> v2(5, 2);
// 3. Create a vector v3 with 3 elements of value 1 and with the allocator of vector v2
std::vector<int> v3(3, 1, v2.get_allocator());
// 4. Create a copy, vector v4, of vector v2
std::vector<int> v4(v2);
// 5. Create a vector v5 by copying the range v4[_First, _Last)
std::vector<int> v5(v4.begin() + 1, v4.begin() + 3);
std::cout << "v1 = ";
std::copy(v1.begin(), v1.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::cout << "v2 = ";
std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::cout << "v3 = ";
std::copy(v3.begin(), v3.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::cout << "v4 = ";
std::copy(v4.begin(), v4.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::cout << "v5 = ";
std::copy(v5.begin(), v5.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
// Move vector v2 to vector v6
std::vector<int> v6(move(v2));
std::cout << "v6 = ";
std::copy(v6.begin(), v6.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
};
multiset 就是可以存放多个相同的元素.就是在删除的时候如果删除元素则删除的是所有元素,如果一个个删除要用迭代器去删除.
ACM中常用来做为二叉树的备用品.查找使用upper_bound和lower_bound这两个函数.
erase删除有两种方法,一种查找到你要的,st.erase(iterator),st.(item)///这种删掉所有该元素
#include<set>
#include<ctime>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 1100000
using namespace std;
int a[10];
int main(){
srand(time(NULL));
multiset<int,less<int> > st;///注意不同,less 升序(默认)
sort(a,a+10,less<int>() );///
for(int i = 0;i<8;i++){
a[i] = rand();
st.insert(a[i]);
printf("%d ",a[i]);
}puts("");
a[9]=a[3];a[8]=a[6];st.insert(a[9]);st.insert(a[8]);
for(auto it = st.begin();it!=st.end();it++)
printf("%d ",*it);puts("");
auto it = st.lower_bound(a[3]);
st.erase(it);
for(auto it = st.begin();it!=st.end();it++)
printf("%d ",*it);
return 0;
}
原文地址:http://blog.csdn.net/gg_gogoing/article/details/45766863