@c++ 中 string的使用方法总结[
STL 是 C++的一部分,因此不用额外安装什么,它被内建在你的编译器之内。
STL 的一个重要特性是将数据和操作分离。数据由容器类别加以管理,操作则由可定制的算法定义。迭代器在两者之间充当“粘合剂”,以使算法可以和容器交互运作
程序员可以不用思考 STL 具体的实现过程,只要能够熟练使用 STL 就 OK 了。这样他们就可以把精力放在程序开发的别的方面。
STL 具有高可重用性,高性能,高移植性,跨平台的优点。
高可重用性:STL 中几乎所有的代码都采用了模板类和模版函数的方式实现,这相比于传统的由函数和类组成的库来说提供了更好的代码重用机会。
高性能:如 map 可以高效地从十万条记录里面查找出指定的记录,因为 map 是采用红黑树的变体实现的。
高移植性:如在项目 A 上用 STL 编写的模块,可以直接移植到项目 B 上。
STL的意思是Standard Template Library,标准模板库可以通过调用STL模板库实现c中的许多函数的直接调用
本周学的是 string
在c++中使用string时需要有头文件 include 与c不同
使用方法:
string类的输入函数:
string str;
cin>>str;
//或下面一种先定义再输入
getline(cin,str);
string库函数:
//字符串长度:length();
string s("wo shi hanjinlun");
cout<<s.length();
//字符个数:size();
string s("rou dan cong ji");
cout<<s.size();
//输出内存大小:capacity();
string s("he dui you pei he de bu shi hen hao");
cout<<s.capacity();
//修改内存大小:reserve();
s.reserve(50);
cout<<s.capacity()<<endl;
//重新设置字符个数:resize();
s.resize(8);
cout<<s.capacity()<<endl;
cout<<s.length()<<endl;
cout<<s.size()<<endl;
cout<<s<<endl;
//输出单个字符函数:at();
string s("My ledal state is AK");
cout<<s.at(1)<<endl;
//修改单个字符函数:at();
string s("My ledal state is AK");
s.at(1)='t';
cout<<s.c_str()<<endl;
//输出全部字符:c_str
string s1="The last two questions";
cout<<s1.c_str();
//插入函数
//任意位置插入:insert();
s1.insert(0," ");
cout<<s1<<endl;
//末尾插入/添加:append();
s.append(s1);
cout<<s<<endl;
//指定函数:assign(); (输出前五个字符)
s1.assign("That is true",5);
cout<<s1<<endl;
//比较字符串大小:compare();
//整串比较(同C语言一样如果str1>str2,返回 1,str1=str2返回 0 ,str1<str2返回 -1)
string str1="abcd";
string str2="bcdefg";
cout<<str1.compare(str2)<<endl;
//部分与整串比较(str1的前两个与str2比较)
string str1="abcd";
string str2="cdefg";
cout<<str1.compare(1,2,str2)<<endl;
//部分与部分比较(str1的前两个与str2的前两个比较)
string str1="abcd";
string str2="cdefg";
cout<<str1.compare(1,2,str2,1,2)<<endl;
//复制函数:copy();
char a[10]={0};
str1.copy(a,4,0);
//4表示选取的个数,0是位置下标
cout<<a<<endl;
string库函数整体代码实现:
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout<<"求串的长度和大小:\n";
string s("ha ha a ha ha");
cout<<s.length()<<endl;
cout<<s.size()<<endl;
cout<<"\n";
cout<<"求容量:\n";
cout<<s.capacity()<<endl;
cout<<"\n";
cout<<"扩容:\n";
s.reserve(50);
cout<<s.capacity()<<endl;
cout<<"\n";
cout<<"修改容量:\n";
s.resize(18);
cout<<s.capacity()<<endl;
cout<<s.length()<<endl;
cout<<s.size()<<endl;
cout<<s<<endl;
cout<<"\n";
cout<<"输出/修改单个字符:\n";
cout<<s.at(1)<<endl;
s.at(1)='O';
cout<<s.c_str()<<endl;
cout<<"\n";
cout<<"全输出:\n";
string s1="wu hu qi fei";
cout<<s1.c_str();
cout<<"\n";
cout<<"任意位置插入:\n";
s1.insert(0," ");
cout<<s1<<endl;
cout<<"\n";
cout<<"末尾插入:\n";
s.append(s1);
cout<<s<<endl;
cout<<"\n";
cout<<"指定输出:\n";
string s2;
s2.assign("zheng fang xing da ye",5);
cout<<s2<<endl;
cout<<"\n";
cout<<"比较函数:\n";
string str1="abcd";
string str2="cdefg";
cout<<str1.compare(str2)<<endl;
cout<<"\n";
cout<<"复制函数:\n";
char a[10]={0};
str1.copy(a,4,0);
cout<<a<<endl;
cout<<"\n";
cout<<"查找函数:\n";
cout<<(int)s.find("1127")<<endl;
cout<<(int)s.find(s1,2)<<endl;
cout<<"\n";
cout<<"查找字符:\n";
cout<<(int)s.find('2')<<endl;
cout<<"\n";
cout<<"返回子串:\n";
cout<<s.substr(38,46);
cout<<"\n";
cout<<"删除部分子串:\n";
s.erase(18,8);
cout<<s<<endl;
cout<<"\n";
cout<<"最终结果为:\n";
s.replace(0,2,"La");
cout<<s<<endl;
}