#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
int arr[] = {1 ,3 ,5 ,4 ,7 ,6 ,0 ,9 ,8 ,2 };
int arr2[] = {1 ,1 ,5 ,2 ,7 ,3 ,10 ,13 ,18 ,13 };
class Less9 {
public :
bool operator () (int value) {
return value < 9 ;
}
};
class Greater10{
public :
bool operator () (int value) {
return value >= 10 ;
}
};
bool greater10(int value)
{
return value > 10 ;
}
int main(int argc, char * argv[])
{
vector <int > v1(arr,arr+10 );
vector <int > ::const_iterator it;
for (it = v1.begin();it != v1.end();++it){
cout <<*it<<" " ;
}
cout <<endl<<"---------------------------" <<endl;
sort(v1.begin(),v1.end());
for (it = v1.begin();it != v1.end();++it){
cout <<*it<<" " ;
}
cout <<endl;
sort(v1.begin(),v1.end(),greater<int >());
for (it = v1.begin();it != v1.end();++it){
cout <<*it<<" " ;
}
cout <<endl;
vector <int > ::iterator fIt;
fIt = find(v1.begin(),v1.end(),4 );
if (fIt == v1.end()) {
cout << "not found in vector" << endl;
}
else {
cout << *fIt <<endl;
}
vector <int > ::iterator EventIterator =find_if (v1.begin(), v1.end(), Less9());
if (EventIterator==v1.end()) {
cout <<"Event not found in list" << endl;
}
else {
cout <<*EventIterator << endl;
}
fIt = adjacent_find(v1.begin(),v1.end());
if (fIt == v1.end()) cout << "not found in vector" << endl;
else cout << *fIt <<endl;
bool bFind = binary_search(v1.begin(),v1.end(),5 );
if (bFind) cout << "binary_search not found in vector" << endl;
else cout << "binary_search found in vector" << endl;
cout <<"Count of 4:" <<count(v1.begin() , v1.end() , 4 )<<endl;
vector <int > v2(arr2,arr2+10 );
vector <int > ::size_type result1 = count_if(v2.begin(), v2.end(), greater10);
cout <<result1<<endl;
result1 = count_if(v2.begin(), v2.end(), Greater10());
cout <<result1<<endl;
for (it = v2.begin();it != v2.end();++it){
cout <<*it<<" " ;
}cout <<endl;
reverse(v2.begin(), v2.end());
for (it = v2.begin();it != v2.end();++it){
cout <<*it<<" " ;
}cout <<endl;
copy (arr+2 , arr + 4 , v2.begin());
for (it = v2.begin();it != v2.end();++it){
cout <<*it<<" " ;
}cout <<endl;
for (it = v1.begin();it != v1.end();++it){
cout <<*it<<" " ;
}cout <<endl;
v1.erase(remove(v1.begin(),v1.end(),2 ), v1.end());
for (it = v1.begin();it != v1.end();++it){
cout <<*it<<" " ;
}cout <<endl;
v1.erase(remove_if(v1.begin(),v1.end(),Less9()), v1.end());
for (it = v1.begin();it != v1.end();++it){
cout <<*it<<" " ;
}cout <<endl;
int arr3[] = {2 ,3 ,1 ,345 ,123 ,56 ,23 ,888 ,67 ,100 };
vector <int > v3(arr3,arr3+10 );
for (it = v3.begin();it != v3.end();++it){
cout <<*it<<" " ;
}cout <<endl;cout <<"-----" <<endl;
vector <int > v4(arr3,arr3+10 );
v4.erase( std ::remove_if( v4.begin(), v4.end(),std ::bind1st( std ::less< int >(), 100 )), v4.end());
for (it = v4.begin();it != v4.end();++it){
cout <<*it<<" " ;
}cout <<endl;cout <<"-----" <<endl;
v3.erase( std ::remove_if( v3.begin(), v3.end(),std ::not1(std ::bind2nd( std ::greater<int >(), 100 ))), v3.end());
for (it = v3.begin();it != v3.end();++it){
cout <<*it<<" " ;
}cout <<endl;
return 0 ;
}
bool divbyfive(int x)
{
return x % 5 ? 0 : 1 ;
}
#include "stdafx.h"
#if 1
#include <vector>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <functional>
using namespace std ;
void f(int (&a)[5 ]);
int main(int argc, char * argv[])
{
cout <<"test..." <<endl;
int arr[] = {1 ,2 ,3 ,4 ,5 };
f(arr);
getchar();
return 0 ;
}
bool comp9(const int &a,const int &b)
{
return a>b;
}
void f(int (&a)[5 ])
{
a[1 ] = 100 ;
for (int i = 0 ; i < 5 ; i++)
cout <<a[i]<<endl;
cout <<"-----------------------" <<endl;
vector <int > vec;
vec.push_back(1 );
vec.push_back(55 );
vec.push_back(3 );
sort(vec.begin(),vec.end());
sort(vec.begin(),vec.end(),greater<int >());
vector <int > ::const_iterator it;
for (it = vec.begin();it != vec.end();it++){
cout <<*it<<endl;
}
cout <<"A---------------------------------" <<endl;
vector <int > lv;
for (i = 0 ; i < 100 ; i++)
{
lv.push_back(i);
}
cout << count_if(lv.begin(), lv.end(), bind2nd(less<int >(), 20 )) << endl;
cout <<"B---------------------------------" <<endl;
sort(lv.begin(), lv.end(), not2(less<int >()) ) ;
for (i = 0 ; i < 100 ; i++)
{
cout << lv.at(i) << endl;
}
}
#else
#include <iostream>
#include <algorithm>
using namespace std ;
#define SIZE 100
int iarray[SIZE];
class Test{
public :
int operator ()(int x,int y){
return x+y;
}
};
template <class T>
T add(T x,T y)
{
return x+y;
}
int main()
{
Test t;
cout <<t(10 ,12 )<<endl;
iarray[20 ] = 50 ;
int * ip = find(iarray, iarray + SIZE, 50 );
if (ip == iarray + SIZE)
cout << "50 not found in array" << endl;
else
cout << *ip << " found in array" << endl;
cout <<add(1 ,2 )<<endl;
cout <<add(1.12 ,2.12 )<<endl;
return 0 ;
}
#endif
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <deque>
using namespace std ;
#if 0
int ar[10 ] = { 12 , 45 , 234 , 64 , 12 , 35 , 63 , 23 , 12 , 55 };
int main(int argc, char * argv[])
{
vector <int > v1(ar, ar+10 );
vector <int > v2(10 ,1 );
v2.assign(10 ,4 );
vector <int > ::const_iterator it;
for (it=v2.begin();it!=v2.end();++it){
cout <<*it<<endl;
}
cout <<v1.front()<<endl;
cout <<v1.size()<<endl;
cout <<v1.back()<<endl;
v1.pop_back();
cout <<v1.back()<<endl;
cout <<v1.at(3 )<<"---" <<v1[3 ]<<endl;
cout <<v1.empty()<<endl;
v1.erase(v1.begin()+1 ,v1.end()-2 );
for (it=v1.begin();it!=v1.end();++it){
cout <<*it<<" " ;
}
cout <<endl;
return 0 ;
}
#else
void printDeque(deque <int > d)
{
deque <int > ::iterator *pIter = new deque <int > ::iterator;
if ( NULL == pIter )
{
return ;
}
for (*pIter = d.begin(); *pIter != d.end(); (*pIter)++)
{
cout <<"d[" <<*pIter - d.begin() <<"]=" <<**pIter<<", " ;
}
if (NULL != pIter)
{
delete pIter;
pIter = NULL;
}
cout <<endl;
}
int main()
{
deque <int > d1;
deque <int > d2(10 );
deque <double > d3(10 , 5.5 );
deque <double > d4(d3);
int iArray[] = {11 , 13 , 19 , 23 , 27 };
deque < int > d5(5 ,12 );
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);
cout <<"其它常用用法: " <<endl;
int flag = 0 ;
while (flag < 2 )
{
if (0 == flag )
{
for (int i = 1 ; i < 6 ; i++)
d1.push_back(i*10 );
}
else
{
d1.clear();
cout <<"after d1.clear() , d1.front(), d1.back() is abnormal! other info.:" <<endl;
}
cout <<"d1.empty() = " <<d1.empty()<<endl;
cout <<"d1.size() = " <<d1.size()<<endl;
cout <<"d1.max_size() = " <<hex<<d1.max_size()<<endl;
if (!d1.empty())
{
cout <<"d1.front() = " <<d1.front()<<endl;
cout <<"d1.back() = " <<d1.back()<<endl;
}
flag++;
}
cout <<"d1.swap(d5)= " <<endl;
d1.swap(d5);
cout <<"d1 = " ;
printDeque(d1);
cout <<"d5 = " ;
printDeque(d5);
}
#endif
#if 0
C++ Lists用法
Lists将元素按顺序储存在链表中. 与向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.
assign() 给list 赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list 是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list 的配置器
insert() 插入一个元素到list 中
max_size() 返回list 能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list 的末尾添加一个元素
push_front() 在list 的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list 删除元素
remove_if() 按指定条件删除元素
rend() 指向list 末尾的逆向迭代器
resize() 改变list 的大小
reverse() 把list 的元素倒转
size() 返回list 中的元素个数
sort() 给list 排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list 中重复的元素
附List用法实例:
#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
using namespace std ;
typedef list <int > LISTINT;
typedef list <char > LISTCHAR;
void main(void )
{
LISTINT listOne;
LISTINT::iterator i;
listOne.push_front (2 );
listOne.push_front (1 );
listOne.push_back (3 );
listOne.push_back (4 );
cout <<"listOne.begin()--- listOne.end():" <<endl;
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " " ;
cout << endl;
LISTINT::reverse_iterator ir;
cout <<"listOne.rbegin()---listOne.rend():" <<endl;
for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
cout << *ir << " " ;
}
cout << endl;
int result = accumulate(listOne.begin(), listOne.end(),0 );
cout <<"Sum=" <<result<<endl;
cout <<"------------------" <<endl;
LISTCHAR listTwo;
LISTCHAR::iterator j;
listTwo.push_front ('A' );
listTwo.push_front ('B' );
listTwo.push_back ('x' );
listTwo.push_back ('y' );
cout <<"listTwo.begin()---listTwo.end():" <<endl;
for (j = listTwo.begin(); j != listTwo.end(); ++j)
cout << char (*j) << " " ;
cout << endl;
j=max_element(listTwo.begin(),listTwo.end());
cout << "The maximum element in listTwo is: " <<char (*j)<<endl;
}
#include <iostream>
#include <list>
using namespace std ;
typedef list <int > INTLIST;
void put_list(INTLIST list , char *name)
{
INTLIST::iterator plist;
cout << "The contents of " << name << " : " ;
for (plist = list .begin(); plist != list .end(); plist++)
cout << *plist << " " ;
cout <<endl;
}
void main(void )
{
INTLIST list1;
INTLIST list2(10 ,6 );
INTLIST list3(list2.begin(),--list2.end());
INTLIST::iterator i;
put_list(list1,"list1" );
put_list(list2,"list2" );
put_list(list3,"list3" );
list1.push_back(2 );
list1.push_back(4 );
cout <<"list1.push_back(2) and list1.push_back(4):" <<endl;
put_list(list1,"list1" );
list1.push_front(5 );
list1.push_front(7 );
cout <<"list1.push_front(5) and list1.push_front(7):" <<endl;
put_list(list1,"list1" );
list1.insert(++list1.begin(),3 ,9 );
cout <<"list1.insert(list1.begin()+1,3,9):" <<endl;
put_list(list1,"list1" );
cout <<"list1.front()=" <<list1.front()<<endl;
cout <<"list1.back()=" <<list1.back()<<endl;
list1.pop_front();
list1.pop_back();
cout <<"list1.pop_front() and list1.pop_back():" <<endl;
put_list(list1,"list1" );
list1.erase(++list1.begin());
cout <<"list1.erase(++list1.begin()):" <<endl;
put_list(list1,"list1" );
list2.assign(8 ,1 );
cout <<"list2.assign(8,1):" <<endl;
put_list(list2,"list2" );
cout <<"list1.max_size(): " <<list1.max_size()<<endl;
cout <<"list1.size(): " <<list1.size()<<endl;
cout <<"list1.empty(): " <<list1.empty()<<endl;
put_list(list1,"list1" );
put_list(list3,"list3" );
cout <<"list1>list3: " <<(list1>list3)<<endl;
cout <<"list1<list3: " <<(list1<list3)<<endl;
list1.sort();
put_list(list1,"list1" );
list1.splice(++list1.begin(), list3);
put_list(list1,"list1" );
put_list(list3,"list3" );
}
#include <vector>
#include <algorithm>
#include <iostream>
class example
{
public :
example(int val)
{
i = val;
}
bool operator ==(example const & rhs)
{
return (i == rhs.i) ? true : false ;
}
private :
int i;
};
using namespace std ;
int main(void )
{
vector <example> ve;
ve.push_back(1 );
vector <example> ::iterator it;
example elem(1 );
it = find(ve.begin(), ve.end(), elem);
cout <<boolalpha<<(*it == elem);
}
#endif