1 容器
數組
鏈表
vector----向量----數組
代碼:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//int array1[10];
//array1[0]=10;
//array1[1]=11;
vector<int> vec1; //定義了1個對象vec1.因爲vector是類模板,所以必須要加<int>;當然,如果你準備放CStudent對象,就應改爲<CStudent>
vec1.push_back(10); //push_back翻譯過來,就是在後面,進行進棧,即追加元素。
vec1.push_back(11);
vec1.push_back(12);
vec1.push_back(13);
vector<int>::iterator it; //定義了1個迭代器變量it。it本質上就是1個地址變量,而且是1個整型地址變量,可以利用它來遍歷元素、指定遊標位置。
it=vec1.begin(); //將遊標走到 第1個元素上。
it++; //遊標走到第2個元素上了。
vec1.erase(it); //刪除 遊標位置的元素
//vec1[0]=10; //這樣的賦值語句,非法。因爲,容器中可能還沒有0號元素呢
//vec1[1]=11;
//cout<<vec1[0]<<endl; //語法正確,也能正常輸出,但不敢這樣用,因爲,有可能還沒有元素0
for(vector<int>::size_type i=0;i!=vec1.size();i++) //這是下標法 遍歷元素。size_type是數據類型,而且有特別的作用域;vec1.size()返回的是實際的元素個數
{
cout<<vec1[i]<<"\t"; //這樣使用下標,是安全的,因爲如果i不合適,就不會執行該語句。
}
cout<<endl;
for(it=vec1.begin();it!=vec1.end();it++) //迭代器法 遍歷數組
{
cout<<*it<<"\t"; //it是地址變量,加了*後,就爲變量了
}
cout<<endl;
return 0;
}
代碼:----容器裏放 學生地址
//main.cpp
#include <iostream>
#include <vector>
#include "Student.h"
using namespace std;
int main()
{
vector<CStudent *> vec1;
CStudent stu1(1001,"zhangsan");
CStudent stu2(1002,"lisi");
CStudent stu3(1003,"wangwu");
vec1.push_back(&stu1);
vec1.push_back(&stu2);
vec1.push_back(&stu3);
for(vector<CStudent *>::size_type i=0;i!=vec1.size();i++)
{
vec1.at(i)->print_student();
}
for(vector<CStudent *>::iterator it=vec1.begin();it!=vec1.end();it++)
{
(*it)->print_student();
}
return 0;
}
//Student.h
#include <iostream>
#include <string>
using namespace std;
class CStudent
{
int number;
string name;
public:
CStudent();
virtual ~CStudent();
CStudent(int number,string name);
void print_student();
};
//Student.cpp
#include "Student.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CStudent::CStudent()
{
}
CStudent::~CStudent()
{
}
CStudent::CStudent(int number,string name)
{
this->number=number;
this->name=name;
}
void CStudent::print_student()
{
cout<<"number is "<<number<<" name is "<<name<<endl;
return;
}
1 算法
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include "Student.h"
using namespace std;
int main()
{
vector <int> vec1;
vec1.push_back(9);
vec1.push_back(3);
vec1.push_back(6);
vec1.push_back(1);
vec1.push_back(7);
sort(vec1.begin(),vec1.end()); //sort算法,要增加頭文件algorithm;默認從小到大排
//reverse(vec1.begin(),vec1.end()); //reverse算法,即反過來排列
//sort(vec1.begin(),vec1.end(),greater<int>()); //使用了仿函數greater,要增加頭文件functional;sort改爲從到小排序了
for(vector<int>::iterator it=vec1.begin();it!=vec1.end();it++) //遍歷容器----迭代器法
{
cout<<(*it)<<" ";
}
cout<<endl;
return 0;
}
使用了find算法的代碼:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include "Student.h"
using namespace std;
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
vector <int> vec1;
vec1.push_back(9);
vec1.push_back(3);
vec1.push_back(6);
vec1.push_back(1);
vec1.push_back(7);
//sort(vec1.begin(),vec1.end()); //sort算法,要增加頭文件algorithm;默認從小到大排
//reverse(vec1.begin(),vec1.end()); //reverse算法,即反過來排列
//sort(vec1.begin(),vec1.end(),greater<int>()); //使用了仿函數greater,要增加頭文件functional;sort改爲從到小排序了
//sort(vec1.begin(),vec1.end(),cmp);
vector<int>::iterator it2=find(vec1.begin(),vec1.end(),68);
if(it2!=vec1.end())
{
cout<<"找到了"<<endl;
cout<<*it2<<endl;
}
/*
for(vector<int>::iterator it=vec1.begin();it!=vec1.end();it++) //遍歷容器----迭代器法
{
cout<<(*it)<<" ";
}*/
cout<<endl;
return 0;
}
容器中放CStudent的sort算法:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include "Student.h"
using namespace std;
bool cmp(CStudent &a,CStudent &b)
{
return a.number<b.number;
}
int main()
{
CStudent stu1(1003,"zhangsan");
CStudent stu2(1001,"lisi");
CStudent stu3(1002,"wangwu");
vector<CStudent> vec1;
vec1.push_back(stu1);
vec1.push_back(stu2);
vec1.push_back(stu3);
sort(vec1.begin(),vec1.end(),cmp);
for(vector<CStudent>::iterator it=vec1.begin();it!=vec1.end();it++)
{
(*it).print_student();
}
return 0;
}
容器中放CStudent的find_if算法:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include "Student.h"
using namespace std;
bool cmp(CStudent &a,CStudent &b)
{
return a.number<b.number;
}
bool isFound(CStudent &other)
{
return other.number==1002;
}
int main()
{
CStudent stu1(1003,"zhangsan");
CStudent stu2(1001,"lisi");
CStudent stu3(1002,"wangwu");
vector<CStudent> vec1;
vec1.push_back(stu1);
vec1.push_back(stu2);
vec1.push_back(stu3);
//sort(vec1.begin(),vec1.end(),cmp);
vector<CStudent>::iterator it2=find_if(vec1.begin(),vec1.end(),isFound);
if(it2!=vec1.end())
{
cout<<"找到了"<<endl;
(*it2).print_student();
}
/*
for(vector<CStudent>::iterator it=vec1.begin();it!=vec1.end();it++)
{
(*it).print_student();
}*/
return 0;
}
函數對象,或對象函數----本質上是 類的第5個成員函數---對象函數---括號運算符重載
//main.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include "Student.h"
using namespace std;
bool cmp(CStudent &a,CStudent &b)
{
return a.number<b.number;
}
bool isFound(CStudent &other)
{
return other.number==1002;
}
void print(CStudent &stu)
{
stu.print_student();
return;
}
int main()
{
CStudent stu1(1003,"zhangsan");
CStudent stu2(1001,"lisi");
CStudent stu3(1002,"wangwu");
vector<CStudent> vec1;
vec1.push_back(stu1);
vec1.push_back(stu2);
vec1.push_back(stu3);
CStudent stu99(1004,"wangwu"); //stu99爲要查找的對象參照----本例中,只要認爲 學號 或 姓名 有1個相同,就算找到了
//vector<CStudent>::iterator it2=find_if(vec1.begin(),vec1.end(),isFound); //isFound會函數名,這是利用 回調函數 來查找;但回調函數中,學號或姓名必須寫死
vector<CStudent>::iterator it2=find_if(vec1.begin(),vec1.end(),stu99); // stu99爲對象名,這是利用 對象函數 來查找;解決了回調函數的缺陷。需要定義對象函數
if(it2!=vec1.end())
{
cout<<"找到了"<<endl;
(*it2).print_student();
}
return 0;
}
//Student.h
#include <iostream>
#include <string>
using namespace std;
class CStudent
{
public:
int number;
string name;
public:
CStudent();
virtual ~CStudent();
CStudent(int number,string name);
void print_student();
bool operator()(CStudent &other); //可以替換爲bool fun(CStudent &other),這樣好理解
};
//Student.cpp
// Student.cpp: implementation of the CStudent class.
//
//////////////////////////////////////////////////////////////////////
#include "Student.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CStudent::CStudent()
{
}
CStudent::~CStudent()
{
}
CStudent::CStudent(int number,string name)
{
this->number=number;
this->name=name;
}
void CStudent::print_student()
{
cout<<"number is "<<number<<" name is "<<name<<endl;
return;
}
bool CStudent::operator()(CStudent &other) //可以替換爲bool CStudent::fun(CStudent &other),這樣好理解
{ //函數功能:就是隻要當前對象的學號或姓名 與other的學號或姓名 相同,就返回true
bool flag;
if(this->number==other.number || this->name==other.name)
{
flag=true;
}
else
{
flag=false;
}
return flag;
}