分享一些c++案例
str简介
容器算法迭代器初识
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
void myPrint(int val)
{
cout<<val<<endl;
}
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
for_each(v.begin(),v.end(),myPrint);
}
int main()
{
test01();
system("pause");
return 0;
}
容器中存放自定义类型
#include <iostream>
using namespace std;
#include<vector>
#include<string>
class Person
{
public:
Person(string name,int age)
{
this->m_Name=name;
this->m_Age=age;
}
string m_Name;
int m_Age;
};
void test01()
{
vector<Person>v;
Person p1("aaa",10);
Person p2("bbb",20);
Person p3("ccc",30);
Person p4("ddd",40);
Person p5("eee",50);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
for(vector<Person>::iterator it=v.begin();it!=v.end();it++)
{
cout<<"姓名: "<<it->m_Name<<" 年龄"<<it->m_Age<<endl;
}
}
void test02()
{
vector<Person*>v;
Person p1("aaa",10);
Person p2("bbb",20);
Person p3("ccc",30);
Person p4("ddd",40);
Person p5("eee",50);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
for(vector<Person*>::iterator it=v.begin();it!=v.end();it++)
{
cout<<"姓名:"<<(*it)->m_Name<<"年龄:"<<(*it)->m_Age<<endl;
}
}
int main()
{
test02();
system("pause");
return 0;
}
简单评委打分
#include <iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
using namespace std;
class Person
{
public:
Person(string name,int score)
{
this->m_Name=name;
this->m_Score=score;
}
string m_Name;
int m_Score;
};
void creatPerson(vector<Person>&v)
{
string nameSeed="ABCDE";
for(int i=0;i<5;i++)
{
string name="选手";
name+=nameSeed[i];
int score=0;
Person p(name,score);
v.push_back(p);
}
}
int main()
{
vector<Person>v;
creatPerson(v);
for(vector<Person>::iterator it=v.begin();it!=v.end();it++)
{
cout<<"姓名为"<<it->m_Name<<"分数为"<<it->m_Score<<endl;
}
system("pause");
return 0;
}
容器嵌套容器
#include <iostream>
using namespace std;
#include<vector>
void test01()
{
vector<vector<int>>v;
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
for(int i=0;i<4;i++)
{
v1.push_back(i+1);
v2.push_back(i+2);
v3.push_back(i+3);
v4.push_back(i+4);
}
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
for(vector<vector<int>>::iterator it =v.begin();it != v.end();it++)
{
for(vector<int>::iterator vit=(*it).begin();vit!=(*it).end();vit++)
{
cout<<*vit<<" ";
}
cout<<endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
map容器的插入和删除
#include <iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
#include<list>
#include<set>
#include<map>
#include<ctime>
using namespace std;
void printMap(map<int,int>&m)
{
for(map<int,int>::iterator it=m.begin();it!=m.end();it++)
{
cout<<"key="<<it->first<<"value="<<it->second<<endl;
}
cout<<endl;
}
void test01()
{
map<int,int>m;
m.insert(pair<int,int>(1,10));
m.insert(make_pair(2,20));
m.insert(map<int,int>::value_type (3,30));
m[4]=40;
printMap(m);
m.erase(m.begin());
printMap(m);
m.erase(3);
printMap(m);
m.erase(m.begin(),m.end());
printMap(m);
}
int main()
{
test01();
system("pause");
return 0;
}
map容器的查找和统计
#include <iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
#include<list>
#include<set>
#include<map>
#include<ctime>
using namespace std;
void test01()
{
map<int,int>m;
m.insert(pair<int,int>(1,10));
m.insert(make_pair(2,20));
m.insert(pair<int,int>(3,30));
m.insert(pair<int,int>(4,40));
map<int,int>::iterator pos=m.find(3);
if(pos!=m.end())
{
cout<<"查到了元素key="<<pos->first<<"value="<<(*pos).second<<endl;
}
else
{
cout<<"没有查找到"<<endl;
}
int num=m.count(3);
cout<<"num="<<num<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
容器排序
#include <iostream>
#include<map>
using namespace std;
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1>v2;
}
};
void test01()
{
map<int,int,MyCompare>m;
m.insert(make_pair(1,10));
m.insert(make_pair(2,20));
m.insert(make_pair(3,30));
m.insert(make_pair(4,40));
m.insert(make_pair(5,50));
for(map<int,int,MyCompare>::iterator it=m.begin();it!=m.end();it++)
{
cout<<"key="<<it->first<<"value="<<it->second<<endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
string基本概念
#include <iostream>
using namespace std;
#include<string>
void test01()
{
string s1;
const char *str="hello world";
string s2(str);
cout<<"s2="<<s2<<endl;
string s3(s2);
cout<<"s3="<<s3<<endl;
string s4(10,'a');
cout<<"s4="<<s4<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string赋值
#include <iostream>
using namespace std;
#include<string>
void test01()
{
string str1;
str1="hello world";
cout<<"str1="<<str1<<endl;
string str2;
str2=str1;
cout<<"str2="<<str2<<endl;
string str3;
str3='a';
cout<<"str3="<<str3<<endl;
string str4;
str4.assign("hello c++");
cout<<"str4="<<str4<<endl;
string str5;
str5.assign("hello c++",5);
cout<<"str5="<<str5<<endl;
string str6;
str6.assign(str5);
cout<<"str6="<<str6<<endl;
string str7;
str7.assign(10,'w');
cout<<"str7="<<str7<<endl;
}
int