string类型
#include <iostream>
using namespace std;
int main()
{
string s="s"; //直接赋值
string s1(s); //s1为s的副本
cout<<s1<<endl;
string s2("s2");
cout<<s2[0]<<s2[1]<<endl; //数组形式储存
string s3(3,'s'); //注意单引号
for (string::size_type i=0; i != s3.size(); ++i) //c++安全泛型编程
cout<<s3[i];
cout<<endl<<"s.empty= "<<s.empty()<<endl; //判断是否为空串
cout<<"s2.size()= "<<s2.size()<<endl; //返回字符个数
string sc=s1+" "+s2+" 1"; //string类型的加法
cout<<"s1 + s2 + \"1\" = "<<sc<<endl;
}
vector 类型
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<string> a (3,"a");
for (vector<string>::size_type i=0; i!=a.size(); ++i)
cout<<a[i];
cout<<endl;
vector<int> b;
for (vector<int>::size_type i=0; i != 5; ++i) //用push_back赋值
{
b.push_back(i);
cout<<b[i]<<" ";
};
cout<<endl;
vector<string> c(3,"b"); //注意双引号
for (vector<string>::iterator iter=c.begin(); iter != c.end(); ++iter) //iterator 迭代器
{
//cout<<*iter;
*iter="c";
cout<<*iter;
};
cout<<endl;
vector<int> nines(3,9);
nines[1]=8;
for (string::size_type i=0 ; i != nines.size(); ++i) //iterator迭代器的算术操作
{
vector<int>::iterator cit2=nines.begin()+i;
cout<<*cit2;
};
cout<<endl;
return 0;
}