标签:class 算法 迭代器 容器
一 常用容器举例
1 vector:
vector类似于动态数组,直接访问元素,从后面快速插入或者删除,示例代码如下:
-
#include <iostream>
-
#include <vector>//包含vector
-
using namespace std;
-
-
int main()
-
{
-
cout<<"----------vector test-----------"<<endl;
-
-
-
vector <int> vect;
-
vector <int> vect1(12);
-
vector <int> vect2(12,9);
-
-
-
-
int a[]={0,1,2,3,4,5,6,7,8,9,0};
-
-
vector <int> vt(a+1,a+4);
-
-
vt.push_back(1);
-
vt.push_back(2);
-
vt.push_back(3);
-
-
-
vector <int>::iterator iter=vt.begin();
-
vector <int>::iterator iter_end=vt.end();
-
-
for(;iter!=iter_end;iter++)
-
{
-
cout<<*iter<<endl;
-
}
-
-
-
vt.pop_back();
-
-
-
iter=vt.begin();
-
iter_end=vt.end();
-
cout<<"----------executed pop_back------"<<endl;
-
for(;iter!=iter_end;iter++)
-
{
-
cout<<*iter<<endl;
-
}
-
-
-
cout<<"----------insert into------------"<<endl;
-
-
vt.insert(vt.begin()+1,3,9);
-
iter=vt.begin();
-
iter_end=vt.end();
-
for(;iter!=iter_end;iter++)
-
{
-
cout<<*iter<<endl;
-
}
-
-
-
cout<<"----------erase-------------------"<<endl;
-
-
-
iter=vt.begin();
-
iter_end=vt.end();
-
vt.erase(iter+1,iter_end);
-
iter_end=vt.end();
-
for(;iter!=iter_end;iter++)
-
{
-
cout<<*iter<<endl;
-
}
-
return 1;
-
}
-
#include <iostream>
-
#include <vector>//包含vector
-
using namespace std;
-
-
int main()
-
{
-
cout<<"----------vector test-----------"<<endl;
-
-
-
vector <int> vect;
-
vector <int> vect1(12);
-
vector <int> vect2(12,9);
-
-
-
-
int a[]={0,1,2,3,4,5,6,7,8,9,0};
-
-
vector <int> vt(a+1,a+4);
-
-
vt.push_back(1);
-
vt.push_back(2);
-
vt.push_back(3);
-
-
-
vector <int>::iterator iter=vt.begin();
-
vector <int>::iterator iter_end=vt.end();
-
-
for(;iter!=iter_end;iter++)
-
{
-
cout<<*iter<<endl;
-
}
-
-
-
vt.pop_back();
-
-
-
iter=vt.begin();
-
iter_end=vt.end();
-
cout<<"----------executed pop_back------"<<endl;
-
for(;iter!=iter_end;iter++)
-
{
-
cout<<*iter<<endl;
-
}
-
-
-
cout<<"----------insert into------------"<<endl;
-
-
vt.insert(vt.begin()+1,3,9);
-
iter=vt.begin();
-
iter_end=vt.end();
-
for(;iter!=iter_end;iter++)
-
{
-
cout<<*iter<<endl;
-
}
-
-
-
cout<<"----------erase-------------------"<<endl;
-
-
-
iter=vt.begin();
-
iter_end=vt.end();
-
vt.erase(iter+1,iter_end);
-
iter_end=vt.end();
-
for(;iter!=iter_end;iter++)
-
{
-
cout<<*iter<<endl;
-
}
-
return 1;
-
}
2 list
list 为双向链表,可以从任何地方插入或者删除的,其示例代码如下:
-
#include <iostream>
-
#include <list>
-
using namespace std;
-
-
void main()
-
{
-
list<int> c1;
-
c1.push_back(1);
-
c1.push_back(2);
-
c1.push_back(3);
-
c1.push_back(4);
-
-
c1.push_front(0);
-
-
c1.pop_back();
-
int& i = c1.back();
-
const int& ii = c1.front();
-
-
-
cout << "The last integer of c1 is " << i << endl;
-
cout << "The front interger of c1 is " << ii << endl;
-
-
cout << "for循环读出数据举例:" << endl;
-
-
list<int>::iterator it;
-
for(it = c1.begin() ; it != c1.end() ;it++)
-
{
-
cout << *it << endl;
-
}
-
system("pause");
-
}
-
#include <iostream>
-
#include <list>
-
using namespace std;
-
-
void main()
-
{
-
list<int> c1;
-
c1.push_back(1);
-
c1.push_back(2);
-
c1.push_back(3);
-
c1.push_back(4);
-
-
c1.push_front(0);
-
-
c1.pop_back();
-
int& i = c1.back();
-
const int& ii = c1.front();
-
-
-
cout << "The last integer of c1 is " << i << endl;
-
cout << "The front interger of c1 is " << ii << endl;
-
-
cout << "for循环读出数据举例:" << endl;
-
-
list<int>::iterator it;
-
for(it = c1.begin() ; it != c1.end() ;it++)
-
{
-
cout << *it << endl;
-
}
-
system("pause");
-
}
3 deque:
deque: 是一个double-ended queue,
1)支持随即存取,也就是[]操作符,
2)支持两端操作,push(pop)-back(front),在两端操作上与list效率差不多
因此在实际使用时,如何选择这三个容器中哪一个,应根据你的需要而定,一般应遵循下面的原则:
1、如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector
2、如果你需要大量的插入和删除,而不关心随即存取,则应使用list
3、如果你需要随即存取,而且关心两端数据的插入和删除,则应使用deque。
示例代码如下:
-
-
-
-
-
-
-
-
-
-
-
#include <iostream>
-
#include <deque>
-
using namespace std;
-
-
void printDeque(const deque<int>& d)
-
{
-
cout<<"\n使用下标:\n";
-
for (unsigned int i = 0; i < d.size(); i++)
-
{
-
cout<<"d["<<i<<"] = "<<d[i]<<", ";
-
}
-
-
-
cout<<"\n使用迭代器\n";
-
deque<int>::const_iterator iter = d.begin();
-
for (;iter != d.end(); iter ++)
-
{
-
cout<<"d["<<iter-d.begin()<<"] = "<<(*iter)<<", ";
-
}
-
cout<<endl;
-
}
-
-
void main()
-
{
-
-
deque<int> d1;
-
deque<int> d2(10);
-
deque<double> d3(10, 5.5);
-
deque<double> d4(d3);
-
-
-
for (int i = 1; i < 6 ; i++)
-
d1.push_back(i*10);
-
-
-
-
cout<<"printDeque(d1) : "<<endl;
-
printDeque(d1);
-
-
-
cout<<"d1.push_front(100): "<<endl;
-
d1.push_front(100);
-
printDeque(d1);
-
cout<<"d1.insert(d1.begin()+3, 200): "<<endl;
-
d1.insert(d1.begin()+2,200);
-
printDeque(d1);
-
-
-
-
cout<<"d1.pop_front(): "<<endl;
-
d1.pop_front();
-
printDeque(d1);
-
-
cout<<"d1.erase(d1.begin()+1): "<<endl;
-
d1.erase(d1.begin()+1);
-
printDeque(d1);
-
-
cout<<"d1.erase(d1.begin(), d1.begin() + 2) = "<<endl;
-
d1.erase(d1.begin(), d1.begin() + 2);
-
printDeque(d1);
-
-
cout<<"d1.clear() :"<<endl;
-
d1.clear();
-
printDeque(d1);
-
-
-
-
}
-
-
-
-
-
-
-
-
-
-
-
#include <iostream>
-
#include <deque>
-
using namespace std;
-
-
void printDeque(const deque<int>& d)
-
{
-
cout<<"\n使用下标:\n";
-
for (unsigned int i = 0; i < d.size(); i++)
-
{
-
cout<<"d["<<i<<"] = "<<d[i]<<", ";
-
}
-
-
-
cout<<"\n使用迭代器\n";
-
deque<int>::const_iterator iter = d.begin();
-
for (;iter != d.end(); iter ++)
-
{
-
cout<<"d["<<iter-d.begin()<<"] = "<<(*iter)<<", ";
-
}
-
cout<<endl;
-
}
-
-
void main()
-
{
-
-
deque<int> d1;
-
deque<int> d2(10);
-
deque<double> d3(10, 5.5);
-
deque<double> d4(d3);
-
-
-
for (int i = 1; i < 6 ; i++)
-
d1.push_back(i*10);
-
-
-
-
cout<<"printDeque(d1) : "<<endl;
-
printDeque(d1);
-
-
-
cout<<"d1.push_front(100): "<<endl;
-
d1.push_front(100);
-
printDeque(d1);
-
cout<<"d1.insert(d1.begin()+3, 200): "<<endl;
-
d1.insert(d1.begin()+2,200);
-
printDeque(d1);
-
-
-
-
cout<<"d1.pop_front(): "<<endl;
-
d1.pop_front();
-
printDeque(d1);
-
-
cout<<"d1.erase(d1.begin()+1): "<<endl;
-
d1.erase(d1.begin()+1);
-
printDeque(d1);
-
-
cout<<"d1.erase(d1.begin(), d1.begin() + 2) = "<<endl;
-
d1.erase(d1.begin(), d1.begin() + 2);
-
printDeque(d1);
-
-
cout<<"d1.clear() :"<<endl;
-
d1.clear();
-
printDeque(d1);
-
-
-
-
}
4 容器适配器:stack
(1)可用 vector, list, deque来实现
(2)缺省情况下,用deque实现
template<classT, class Cont = deque<T> >
class stack { ….. };
(3)用 vector和deque实现,比用list实现性能好
(4)stack是后进先出的数据结构,
(5)只能插入、删除、访问栈顶的元素的操作: push: 插入元素pop: 弹出元素 top: 返回栈顶元素的引用
测试代码如下:
-
<SPAN style="FONT-SIZE: 16px">#include <iostream>
-
#include <Stack>
-
using namespace std;
-
-
void main()
-
{
-
stack<double> s;
-
for( int i=0; i < 10; i++ )
-
s.push(i);
-
while(!s.empty())
-
{
-
printf("%lf\n",s.top());
-
s.pop();
-
}
-
cout << "the size of s: " << s.size() << endl;
-
}
-
</SPAN>
-
<span style="font-size:16px">#include <iostream>
-
#include <Stack>
-
using namespace std;
-
-
void main()
-
{
-
stack<double> s;
-
for( int i=0; i < 10; i++ )
-
s.push(i);
-
while(!s.empty())
-
{
-
printf("%lf\n",s.top());
-
s.pop();
-
}
-
cout << "the size of s: " << s.size() << endl;
-
}
-
</span>
5 deque
可以用 list和deque实现,缺省情况下用deque实现
template<class T, class Cont = deque<T>>
class queue { … };
FIFO先进先出的数据结构,也有push,pop,top函数,但是push发生在队尾,pop,top发生在队头,
示例代码如下:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#include <iostream>
-
#include <queue>
-
#include <assert.h>
-
-
using namespace std;
-
-
int main()
-
{
-
queue <int> myQ;
-
int i;
-
cout<< "现在 queue 是否 empty? "<< myQ.empty() << endl;
-
-
for( i =0; i<10 ; i++)
-
{
-
myQ.push(i);
-
}
-
for( i=0; i<myQ.size(); i++)
-
{
-
printf("myQ.size():%d\n",myQ.size());
-
cout << myQ.front()<<endl;
-
myQ.pop();
-
}
-
-
system("PAUSE");
-
-
return 0;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#include <iostream>
-
#include <queue>
-
#include <assert.h>
-
-
using namespace std;
-
-
int main()
-
{
-
queue <int> myQ;
-
int i;
-
cout<< "现在 queue 是否 empty? "<< myQ.empty() << endl;
-
-
for( i =0; i<10 ; i++)
-
{
-
myQ.push(i);
-
}
-
for( i=0; i<myQ.size(); i++)
-
{
-
printf("myQ.size():%d\n",myQ.size());
-
cout << myQ.front()<<endl;
-
myQ.pop();
-
}
-
-
system("PAUSE");
-
-
return 0;
-
}
二 常用算法
1 count()and count_if()
count()在序列中统计某个值出现的次数
count_if()在序列中统计与某谓词匹配的次数
示例代码如下:
2 find and find_if
find 在序列中找出某个值的第一次出现的位置
find_if 在序列中找出符合某谓词的第一个元素
示例代码如下:
-
#include <algorithm>
-
#include <iostream>
-
-
using namespace std;
-
-
-
void FindFuc()
-
{
-
const int ARRAY_SIZE = 8 ;
-
int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 } ;
-
-
int *location ;
-
-
-
int i ;
-
-
int value = 4 ;
-
-
-
cout << "IntArray { " ;
-
for(i = 0; i < ARRAY_SIZE; i++)
-
cout << IntArray[i] << ", " ;
-
cout << " }" << endl ;
-
-
-
-
location = find(IntArray, IntArray + ARRAY_SIZE, value) ;
-
-
-
if (location != IntArray + ARRAY_SIZE)
-
cout << "First element that matches " << value
-
<< " is at location " << location - IntArray << endl;
-
else
-
-
cout << "The sequence does not contain any elements"
-
<< " with value " << value << endl ;
-
}
-
-
int IsOdd( int n)
-
{
-
return n % 2 ;
-
}
-
-
-
void FindIfFuc()
-
{
-
const int ARRAY_SIZE = 8 ;
-
int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 } ;
-
int *location ;
-
-
int i ;
-
-
-
cout << "IntArray { " ;
-
for(i = 0; i < ARRAY_SIZE; i++)
-
cout << IntArray[i] << ", " ;
-
cout << " }" << endl ;
-
-
-
-
location = find_if(IntArray, IntArray + ARRAY_SIZE, IsOdd) ;
-
-
-
-
if (location != IntArray + ARRAY_SIZE)
-
cout << "First odd element " << *location
-
<< " is at location " << location - IntArray << endl;
-
else
-
cout << "The sequence does not contain any odd numbers"
-
<< endl ;
-
-
}
-
-
-
void main()
-
{
-
FindFuc();
-
FindIfFuc();
-
}
-
#include <algorithm>
-
#include <iostream>
-
-
using namespace std;
-
-
-
void FindFuc()
-
{
-
const int ARRAY_SIZE = 8 ;
-
int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 } ;
-
-
int *location ;
-
-
-
int i ;
-
-
int value = 4 ;
-
-
-
cout << "IntArray { " ;
-
for(i = 0; i < ARRAY_SIZE; i++)
-
cout << IntArray[i] << ", " ;
-
cout << " }" << endl ;
-
-
-
-
location = find(IntArray, IntArray + ARRAY_SIZE, value) ;
-
-
-
if (location != IntArray + ARRAY_SIZE)
-
cout << "First element that matches " << value
-
<< " is at location " << location - IntArray << endl;
-
else
-
-
cout << "The sequence does not contain any elements"
-
<< " with value " << value << endl ;
-
}
-
-
int IsOdd( int n)
-
{
-
return n % 2 ;
-
}
-
-
-
void FindIfFuc()
-
{
-
const int ARRAY_SIZE = 8 ;
-
int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 } ;
-
int *location ;
-
-
int i ;
-
-
-
cout << "IntArray { " ;
-
for(i = 0; i < ARRAY_SIZE; i++)
-
cout << IntArray[i] << ", " ;
-
cout << " }" << endl ;
-
-
-
-
location = find_if(IntArray, IntArray + ARRAY_SIZE, IsOdd) ;
-
-
-
-
if (location != IntArray + ARRAY_SIZE)
-
cout << "First odd element " << *location
-
<< " is at location " << location - IntArray << endl;
-
else
-
cout << "The sequence does not contain any odd numbers"
-
<< endl ;
-
-
}
-
-
-
void main()
-
{
-
FindFuc();
-
FindIfFuc();
-
}
3 for_each()
函数声明如下:
template<class InIt, class Fun>
Fun for_each(InIt first, InIt last, Fun f);
在区间【first,last)上的每个元素执行f操作
示例代码如下:
-
#include <iostream>
-
#include <vector>
-
#include <algorithm>
-
-
using namespace std;
-
-
-
-
void PrintCube(int n)
-
{
-
cout << n * n * n << " " ;
-
}
-
-
void main()
-
{
-
const int VECTOR_SIZE = 8 ;
-
-
-
typedef vector<int > IntVector ;
-
-
-
typedef IntVector::iterator IntVectorIt ;
-
-
IntVector Numbers(VECTOR_SIZE) ;
-
-
IntVectorIt start, end, it ;
-
-
int i ;
-
-
-
for (i = 0; i < VECTOR_SIZE; i++)
-
Numbers[i] = i + 1 ;
-
-
start = Numbers.begin() ;
-
-
-
end = Numbers.end() ;
-
-
-
-
cout << "Numbers { " ;
-
for(it = start; it != end; it++)
-
cout << *it << " " ;
-
cout << " }\n" << endl ;
-
-
-
-
for_each(start, end, PrintCube) ;
-
cout << "\n\n" ;
-
}
-
#include <iostream>
-
#include <vector>
-
#include <algorithm>
-
-
using namespace std;
-
-
-
-
void PrintCube(int n)
-
{
-
cout << n * n * n << " " ;
-
}
-
-
void main()
-
{
-
const int VECTOR_SIZE = 8 ;
-
-
-
typedef vector<int > IntVector ;
-
-
-
typedef IntVector::iterator IntVectorIt ;
-
-
IntVector Numbers(VECTOR_SIZE) ;
-
-
IntVectorIt start, end, it ;
-
-
int i ;
-
-
-
for (i = 0; i < VECTOR_SIZE; i++)
-
Numbers[i] = i + 1 ;
-
-
start = Numbers.begin() ;
-
-
-
end = Numbers.end() ;
-
-
-
-
cout << "Numbers { " ;
-
for(it = start; it != end; it++)
-
cout << *it << " " ;
-
cout << " }\n" << endl ;
-
-
-
-
for_each(start, end, PrintCube) ;
-
cout << "\n\n" ;
-
}
4 unique
unique --常用来删除重复的元素,将相邻的重复的元素移到最后,返回一个iterator指向最后的重复元素,所以用它来删除重复元素时必须先排序
示例代码如下:
-
#include <iostream>
-
#include <algorithm>
-
#include <vector>
-
#include <string>
-
using namespace std;
-
-
-
void main()
-
{
-
string str;
-
vector<string> words;
-
while(cin>>str&&str!="#")
-
{
-
words.push_back(str);
-
}
-
-
sort(words.begin(),words.end());
-
vector<string>::iterator end_unique =
-
unique(words.begin(),words.end());
-
words.erase(end_unique,words.end());
-
-
-
vector<string> ::iterator ite=words.begin();
-
for(;ite!=words.end();ite++)
-
{
-
cout<<*ite<<" ";
-
}
-
-
cout<<endl;
-
}
-
#include <iostream>
-
#include <algorithm>
-
#include <vector>
-
#include <string>
-
using namespace std;
-
-
-
void main()
-
{
-
string str;
-
vector<string> words;
-
while(cin>>str&&str!="#")
-
{
-
words.push_back(str);
-
}
-
-
sort(words.begin(),words.end());
-
vector<string>::iterator end_unique =
-
unique(words.begin(),words.end());
-
words.erase(end_unique,words.end());
-
-
-
vector<string> ::iterator ite=words.begin();
-
for(;ite!=words.end();ite++)
-
{
-
cout<<*ite<<" ";
-
}
-
-
cout<<endl;
-
}
5 常用排序算法
常用排列算法如下:
1 sort 对给定区间所有元素进行排序
2 stable_sort 对给定区间所有元素进行稳定排序
3 partial_sort 对给定区间所有元素部分排序
4 partial_sort_copy 对给定区间复制并排序
示例代码如下:
-
#include <iostream>
-
#include <algorithm>
-
#include <stdlib.h>
-
#include <time.h>
-
#include <VECTOR>
-
using namespace std;
-
-
-
const int N=10;
-
-
void print(const vector<int>& v)
-
{
-
vector<int>::const_iterator ite=v.begin();
-
for(;ite!=v.end();ite++)
-
{
-
cout<<*ite<<" ";
-
}
-
-
cout<<endl;
-
}
-
-
void Create(vector<int>& v)
-
{
-
srand((unsigned int)time(NULL));
-
v.resize(N);
-
for(int i=0;i<N;i++)
-
v[i]=rand()%100;
-
}
-
-
-
-
-
-
-
-
-
void sort1(vector<int> v)
-
{
-
sort(v.begin(),v.end());
-
cout<<"after sort funtion:\n";
-
print(v);
-
}
-
-
void sort2(vector<int> v)
-
{
-
stable_sort(v.begin(),v.end());
-
cout<<"after stable_sort funtion:\n";
-
print(v);
-
}
-
-
void sort3(vector<int> v)
-
{
-
partial_sort(v.begin(),v.begin()+v.size()/2,v.end());
-
cout<<"after partial_sort funtion:\n";
-
print(v);
-
}
-
-
void sort4(vector<int> v)
-
{
-
vector<int> temp;
-
temp.resize(v.size());
-
partial_sort_copy(v.begin(),v.end(),temp.begin(),temp.end());
-
cout<<"after partial_sort_copy funtion:\n";
-
print(temp);
-
}
-
-
-
void main()
-
{
-
vector<int> v;
-
-
Create(v);
-
cout<<"before sort:\n";
-
print(v);
-
-
sort1(v);
-
sort2(v);
-
sort3(v);
-
sort4(v);
-
}
-
#include <iostream>
-
#include <algorithm>
-
#include <stdlib.h>
-
#include <time.h>
-
#include <VECTOR>
-
using namespace std;
-
-
-
const int N=10;
-
-
void print(const vector<int>& v)
-
{
-
vector<int>::const_iterator ite=v.begin();
-
for(;ite!=v.end();ite++)
-
{
-
cout<<*ite<<" ";
-
}
-
-
cout<<endl;
-
}
-
-
void Create(vector<int>& v)
-
{
-
srand((unsigned int)time(NULL));
-
v.resize(N);
-
for(int i=0;i<N;i++)
-
v[i]=rand()%100;
-
}
-
-
-
-
-
-
-
-
-
void sort1(vector<int> v)
-
{
-
sort(v.begin(),v.end());
-
cout<<"after sort funtion:\n";
-
print(v);
-
}
-
-
void sort2(vector<int> v)
-
{
-
stable_sort(v.begin(),v.end());
-
cout<<"after stable_sort funtion:\n";
-
print(v);
-
}
-
-
void sort3(vector<int> v)
-
{
-
partial_sort(v.begin(),v.begin()+v.size()/2,v.end());
-
cout<<"after partial_sort funtion:\n";
-
print(v);
-
}
-
-
void sort4(vector<int> v)
-
{
-
vector<int> temp;
-
temp.resize(v.size());
-
partial_sort_copy(v.begin(),v.end(),temp.begin(),temp.end());
-
cout<<"after partial_sort_copy funtion:\n";
-
print(temp);
-
}
-
-
-
void main()
-
{
-
vector<int> v;
-
-
Create(v);
-
cout<<"before sort:\n";
-
print(v);
-
-
sort1(v);
-
sort2(v);
-
sort3(v);
-
sort4(v);
-
}
6 生成全排列
next_permutation()的原型如下:
template<class BidirectionalIterator>
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
template<class BidirectionalIterator, class BinaryPredicate>
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last,
BinaryPredicate _Comp
);
两个重载函数,第二个带谓词参数_Comp,其中只带两个参数的版本,默认谓词函数为"小于".,返回值为bool类型
示例代码如下:
-
#include <iostream>
-
#include <algorithm>
-
using namespace std;
-
-
void permutation(char* str,int length)
-
{
-
sort(str,str+length);
-
do
-
{
-
for(int i=0;i<length;i++)
-
cout<<str[i];
-
cout<<endl;
-
}while(next_permutation(str,str+length));
-
-
}
-
int main(void)
-
{
-
char str[] = "acb";
-
cout<<str<<"所有全排列的结果为:"<<endl;
-
permutation(str,3);
-
system("pause");
-
return 0;
-
}
常用STL容器及算法举例
标签:class 算法 迭代器 容器
原文地址:http://blog.csdn.net/smartartisan/article/details/44360979