#include <iostream>
#include <vector>
using namespace std;
void main_1()
{
vector<int> vecIntA;//預設構造函數
vector<int> vecIntB(vecIntA);//調用拷貝構造函數
int iSize=vecIntA.size();//放回容器中元素的個數
bool isEmpty=vecIntA.empty();//判斷容器是否為空
//4.比較操作
bool isEqual= (vecIntB == vecIntA);
bool isNotEqual= (vecIntB != vecIntA);
cout<< iSize <<endl;
cout<< isEmpty <<endl;
cout<< isEqual <<endl;
cout<< isNotEqual <<endl;
vecIntA.push_back(2);
cout<<"===================="<<endl;
{
int iSize=vecIntA.size();//放回容器中元素的個數
bool isEmpty=vecIntA.empty();//判斷容器是否為空
//4.比較操作
bool isEqual= (vecIntB == vecIntA);
bool isNotEqual= (vecIntB != vecIntA);
cout<< iSize <<endl;
cout<< isEmpty <<endl;
cout<< isEqual <<endl;
cout<< isNotEqual <<endl;
}
/*
0
1
1
0
====================
1
0
0
1
Press any key to continue
*/
}
#include<algorithm>
#include<numeric>
#include<functional>
bool greaterThan3(int iNum)
{
if(iNum >3)
{
return true;
}
else
{
return false;
}
}
void main_2()
{
int data[] = {1,2,4,5,6,8,6,11};
vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
int iCount = count_if(vecInt.begin(), vecInt.end(), greaterThan3);
cout<<iCount<<endl;
/*
6
Press any key to continue
*/
}
void main_3()
{
//bianry_search();//在有序序列中查找value,找到則傳回true。注意:在無序序列中,不可使用。
//例:
int data[] = {1,2,4,5,6,6,8,11};
vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
bool bFind = binary_search(vecInt.begin(), vecInt.end(), 8);
cout<<bFind<<endl;
bFind = binary_search(vecInt.begin(), vecInt.end(), 10);
cout<<bFind<<endl;
/*
1
0
Press any key to continue
*/
{
int data[] = {1,8,2,4,5,6,6,11};
vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
bool bFind = binary_search(vecInt.begin(), vecInt.end(), 8);
cout<<bFind<<endl;
bFind = binary_search(vecInt.begin(), vecInt.end(), 10);
cout<<bFind<<endl;
/*
0
0
Press any key to continue
*/
}
}
void main_4()
{
//查找指定元素個數:
//count();//利用等于操作符,把标志範圍内的元素與輸入值比較,傳回相等的個數。
//例:
int data[] = {1,2,4,5,6,8,6,11};
vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
int iCount = count(vecInt.begin(), vecInt.end(), 6);
cout<<iCount<<endl;
/*
2
Press any key to continue
*/
}
void main_5()
{
//count_if:();//利用輸入的函數,對标志範圍内的元素進行比較操作,傳回結果為true的個數。
//例:
int data[] = {1,2,4,5,6,8,6,11};
vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
int iCount = count_if(vecInt.begin(), vecInt.end(), greaterThan3);
cout<<iCount<<endl;
/*
6
Press any key to continue
*/
}
void main_6()
{
//條件查找
//find_if();//查找滿足條件的元素位置
//例:
int data[] = {1,2,4,5,6,3,6,11};
vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
vector<int>::iterator it = find_if(vecInt.begin(), vecInt.end(), greaterThan3);
while(it != vecInt.end())
{
cout<<*it<<endl;
it = find_if(it+1, vecInt.end(), greaterThan3);
}
/*
4
5
6
6
11
Press any key to continue
*/
}
void show(const int &item)
{
cout << item << " ";
}
int increase(int item)
{
return item + 1;
}
void main_7()
{
//2、常用合并算法
//加集:
//merge()//合并:合并容器A和B到容器C。
//例:
int arry[]={1,3,5,7,9};
vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );//預設構造函數
vector<int> vecIntB(vecIntA);//調用拷貝構造函數
vector<int> vecIntBB;
vecIntBB.resize(vecIntB.size());
transform(vecIntB.begin(), vecIntB.end(),vecIntBB.begin(), increase);
vector<int> vecIntC;
vecIntC.resize( vecIntA.size() + vecIntB.size() );
//merge(vecIntA.begin(),vecIntA.end(),vecIntB.begin(),vecIntB.end(), vecIntC.begin());
merge(vecIntA.begin(),vecIntA.end(),vecIntBB.begin(),vecIntBB.end(), vecIntC.begin());
for_each(vecIntC.begin(), vecIntC.end(), show);
cout<<endl;
/*
1 2 3 4 5 6 7 8 9 10
Press any key to continue
*/
}
class Student
{
public:
Student():m_id(0),m_name(""){}
Student(int id, string name):m_id(id),m_name(name){}
Student(const Student & stu):m_id(stu.m_id),m_name(stu.m_name){}
public:
int m_id;
string m_name;
};
bool compare(const Student &stuA, const Student &stuB)
{
return stuA.m_id<stuB.m_id ? true : false;
}
void main_8()
{
//3、常用其他算法
//排序:
//sort()//預設升序排序
//例:
//sort(vec.begin(), vec.end())
//自定義排序:
//sort(beg,end,compare)//按照自定義的規則排序
//例:
//srand(time(0));//随機數發生器的初始化
vector<Student> vecStudent;
vecStudent.push_back(Student(3, "小明3"));
vecStudent.push_back(Student(5, "小明5"));
vecStudent.push_back(Student(2, "小明2"));
vecStudent.push_back(Student(1, "小明1"));
vecStudent.push_back(Student(4, "小明4"));
sort(vecStudent.begin(), vecStudent.end(), compare);
vector<Student>::iterator it;
for(it=vecStudent.begin(); it!=vecStudent.end(); it++)
{
cout << (*it).m_name.c_str() << " ";
}
cout<<endl;
/*
小明1 小明2 小明3 小明4 小明5
Press any key to continue
*/
}
void main_9()
{
//颠倒順序:
//reverse();//反轉原有排序
//reverse(vec.begin(), vec.end());
int arry[]={1,3,5,7,9};
vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );//預設構造函數
reverse(vecIntA.begin(), vecIntA.end());
for_each(vecIntA.begin(), vecIntA.end(), show);
cout<<endl;
/*
9 7 5 3 1
Press any key to continue
*/
}
void main_10()
{
//拷貝:
// copy();//拷貝容器A的指定區間到容器B
//例:
vector<Student> vecStudent;
vector<Student> vecStu;
vecStudent.push_back(Student(3, "小明3"));
vecStudent.push_back(Student(5, "小明5"));
vecStudent.push_back(Student(2, "小明2"));
vecStudent.push_back(Student(1, "小明1"));
vecStudent.push_back(Student(4, "小明4"));
vecStu.resize(vecStudent.size());//需要有預設的構造函數Student()
copy(vecStudent.begin(), vecStudent.end(), vecStu.begin());
vector<Student>::iterator it;
for(it=vecStu.begin(); it!=vecStu.end(); it++)
{
cout<<(*it).m_name.c_str()<<endl;
}
/*
小明3
小明5
小明2
小明1
小明4
Press any key to continue
*/
}
void main_11()
{
//替換:
//replace();//将指定元素替換成給定的元素
//replace(vec.begin(), vec.end(), 3, 10);//将容器中所用等于3的元素替換成10
//條件替換:
//replace_if();//替換滿足條件的元素
//例:
int data[] = {1,2,4,5,6,3,6,11};
vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
vector<int>::iterator it;
for(it=vecInt.begin(); it!=vecInt.end(); it++)
{
cout<<*it<<" ";
}
cout<<endl;
replace_if(vecInt.begin(), vecInt.end(), greaterThan3, 10);
for(it=vecInt.begin(); it!=vecInt.end(); it++)
{
cout<<*it<<" ";
}
cout<<endl;
replace(vecInt.begin(), vecInt.end(), 10,3);
for(it=vecInt.begin(); it!=vecInt.end(); it++)
{
cout<<*it<<" ";
}
cout<<endl;
/*
1 2 4 5 6 3 6 11
1 2 10 10 10 3 10 10
1 2 3 3 3 3 3 3
Press any key to continue
*/
}
void main_12()
{
//交換:
//swap(vec1,vec2);//交換容器元素
int arry[]={1,3,5,7,9};
vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );//預設構造函數
vector<int> vecIntB(vecIntA);
transform(vecIntA.begin(), vecIntA.end(),vecIntB.begin(),increase);
swap(vecIntA,vecIntB);
for_each(vecIntA.begin(), vecIntA.end(),show);
cout<<endl;
for_each(vecIntB.begin(), vecIntB.end(),show);
cout<<endl;
/*
2 4 6 8 10
1 3 5 7 9
Press any key to continue
*/
}
void main_13()
{
//計算和:
//accumulate(vec.begin(), vec.end(), 100);//計算從vec.begin()到vec.end()的和再加上最後的100
int arry[]={1,3,5,7,9};
vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );
int sum=accumulate(vecIntA.begin(), vecIntA.end(), 100);
cout<<sum<<endl;
/*
125
Press any key to continue
*/
}
void main()
{
// 填充:
// fill(vec.begin(), vec.end(), 100);//将vec裡的值全部填充成100
vector<int> vecInt;
vecInt.resize(12);
fill(vecInt.begin(), vecInt.end(), 100);
for_each(vecInt.begin(), vecInt.end(),show);
cout<<endl;
/*
100 100 100 100 100 100 100 100 100 100 100 100
Press any key to continue
*/
}