目录
一、std::string容器
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "******************构造初始化******************" << endl;
string str = "ssssss"; //创建一个空字符串
const char* st = "1234"; //创建一个字符串
string str1("hello world"); //构造函数
string str2(3,'ss'); //构造函数
string str3(str); //构造函数
string str4(st); //构造函数
cout << "strl = " << str1 << endl;
cout << "str2 = " << str2 << endl;
cout << "str3 = " << str3 << endl;
cout << "str4 = " << str4 << endl;
/************************************/
cout << "******************存入字符串******************" << endl;
//存入字符串
string str5 = "hello world assign";
string str6,str7,str8,str9,str10;
str6.assign(str5);
str7.assign("hello world assign");
str8.assign(str5.c_str(),str5.size()); //好用
str9.assign(str5,0,str5.size()); //好用
str10.assign(3,'s');
cout << "str5 = " << str5 << endl;
cout << "str6 = " << str6 << endl;
cout << "str7 = " << str7 << endl;
cout << "str8 = " << str8 << endl;
cout << "str9 = " << str9 << endl;
cout << "str10 = " << str10 << endl;
/************************************/
cout << "******************拼接字符串******************" << endl;
//拼接字符串
string str11,str12,str13,str14,str15;
str11 = "ssssss";
str11 += str5;
cout<<"str11 = "<<str11<<endl;
str12.append(str5);
str13.append(10,'3');
str14.append(str5,0,str5.size()); //好用
str15.append(str5,10);
cout << "str11 = " << str11 << endl;
cout << "str12 = " << str12 << endl;
cout << "str13 = " << str13 << endl;
cout << "str14 = " << str14 << endl;
cout << "str15 = " << str15 << endl;
/************************************/
cout << "******************查找字符串******************" << endl;
//查找字符串
string str16,str17,str18,str19,str20;
str16 = 'a';
cout << "查找a:" << str5.find(str16, 0) << endl; //查字符
str17 = "si";
cout << "查找si:" << str5.find(str17, 0) << endl; //查字符串
/************************************/
cout << "******************比较字符串******************" << endl;
//比较字符串
string str21;
str21 = "hello";
if (str5>str21)
{ cout << "str5 > str21" << endl; }
else cout << "str5 < str21" << endl;
/************************************/
cout << "******************提取子串******************" << endl;
//提取子串
string str22 = "wor",str23,str24;
for (int j = 0; j <= str22.size(); j++)
{
for (int i = 0; i <= str5.size(); i++)
{
if (str22[j] == str5[i])
{
str23 += str5[i];
j++;
}
}
}
int num = str5.find("w",0); //提取子串
str24 = str5.substr(num,str22.size()); //提取子串
cout << "str5 size=" << str5.size() << endl;
cout << "str22 size=" << str22.size() << endl;
cout << "str23 size=" << str23.size() << endl;
cout << "str23 = " << str23 << endl;
cout << "str24 = " << str24 << endl;
cout << "******************插入和删除字符串******************" << endl;
//插入和删除字符串
string str25, str26, str27;
str25="hello world",str26="123";
str25.insert(6,"sss"); //从第六个位置开始插入字符串
cout << "str25 = " << str25 << endl;
str25.insert(0,str26); //从第一个位置开始插入字符串
cout << "str25 = " << str25 << endl;
str25.erase(0,5); //删除第一个位置到第五个位置的字符串
cout << "str25 = " << str25 << endl;
str25.erase(0,str25.size()); //清空
cout << "str25 = " << str25 << endl;
}
运行结果:
二、std::vector容器
#include <iostream>
#include <string>
#include <vector> //容器
#include <algorithm>
using namespace std;
void printvector(vector<int> &v) //打印容器
{
vector<int>::iterator it; //迭代器
for (it=v.begin(); it!=v.end(); it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
int main()
{
/******************构造函数*************************/
vector<int> v1(10,20); //数量10,值20
printvector(v1);
vector<int> v2=v1; //拷贝构造函数
printvector(v2);
vector<int> v3(v1.begin(), v1.end()); //拷贝构造函数
printvector(v3);
/*********************赋值**************************/
vector<int> v4;
v4.assign(v3.begin(), v3.end()); //赋值
printvector(v4);
v4.assign(5,100); //赋值
printvector(v4);
/*********************交换**************************/
v3.swap(v4); //交换
printvector(v3);
/******************判断是否为空*********************/
vector<int> v5;
if (v5.empty()) {cout<<"空"<<endl;} else cout<<"非空"<<endl;
/********************改变容器大小*******************/
vector<int> v6(10,20); //容器长度10,值20
cout<<"大小:"<<v6.size()<<"容量:" << v6.capacity()<<endl;
printvector(v6);
v6.resize(15); //改变大小,补0
cout<<"大小:"<<v6.size()<<"容量:" << v6.capacity()<<endl;
printvector(v6);
v6.resize(10); //容量不变
cout<<"大小:"<<v6.size()<<"容量:" << v6.capacity()<<endl;
printvector(v6);
/***********************存取************************/
vector<int> v7;
v7.push_back(1); //尾部插入
v7.push_back(2);
v7.push_back(3);
v7.push_back(4);
v7.at(0)=100; v7[1]=200; //直接赋值改变
printvector(v7);
cout<<v7.at(0)<<" "<<v7[1]<<endl;
cout<<v7.front()<<" "<<v7.back()<<endl; //头尾
/*********************插入删除**********************/
v7.insert(v7.begin()+1,3,2); //插入3个2
printvector(v7);
v7.pop_back(); //删除尾
printvector(v7);
v7.erase(v7.begin() + 1, v7.begin() + 4); //删除+1~+4
printvector(v7);
v7.clear(); //清空
cout<<"大小:"<<v7.size()<<"容量:"<<v7.capacity()<<endl;
/*********************swap收缩空间**********************/
vector<int> v8;
v8.reserve(100); //设置容量
v8.assign(5, 10);
cout<<"大小:"<<v8.size()<<"容量:"<<v8.capacity()<<endl;
vector<int> (v8).swap(v8); //利用匿名对象和函数实现更改容量大小
cout<<"大小:"<<v8.size()<<"容量:"<<v8.capacity()<<endl;
/*********************嵌套容器***********************/
vector<vector<int>> v;
vector<int> v9(3,20);
vector<int> v10(5,30);
vector<int> v11(7, 40);
vector<vector<int>> ::iterator it;
for (it=v.begin(); it!=v.end(); it++)
{
vector<int>::iterator mit;
for (mit=(*it).begin(); mit!=it->end(); mit++)
{
cout<<*mit<<" ";
}
}
/*********************排序***********************/
vector<int> v12;
v12.push_back(1);
v12.push_back(3);
v12.push_back(2);
v12.push_back(4);
printvector(v12); //原始
sort(v12.begin(), v12.end()); //升序
printvector(v12);
sort(v12.begin(), v12.end(), greater<int>()); //降序
printvector(v12);
}
运行结果: