string类的常用接口说明
创始人
2024-02-27 16:27:39
0

STL六大组件:

容器 

算法

配接器

迭代器

仿函数

空间配置器

温馨提示:只讲常用接口,使用方法说明详见代码注释

目录

一、string类对象的常见构造

二、string类对象的容量操作

三、类对象的访问及遍历操作

四、string类对象的修改操作

五、string类非成员函数

六、汇总代码


一、string类对象的常见构造

#include
using namespace std;
#include
//typedef basic_string u16string;std::string s0("Initial string");//构造空的string类对象,即空字符串(默认构造)
//default (1)	explicit basic_string(const allocator_type& alloc = allocator_type());
std::string s1;//用C-string来构造string类对象
//copy(2)    basic_string(const basic_string& str);
std::string s2(s0);//拷贝构造函数
//copy(2)    basic_string(const basic_string& str);
std::string s2(s0);void Teststring()
{string s1;//构造空的string类对象s1string s2("hello sunlang");//用C格式字符串构造string类对象s2string s3(s2);//拷贝构造s3
}

二、string类对象的容量操作

//返回字符串有效字符长度
//size_type size() const;
int main()
{std::string str("Test string");std::cout << "The size of str is" << str.size() << "characters.\n";return 0;
}
//检测字符串释放为空串,是返回true,否则返回false
//bool empty() const;
int main()
{std::string content;std::string line;std::cout << "Please introduce a text.Enter an empty line to finish:\n";do{getline(std::cin, line);content += line + '\n';} while (!line.empty());std::cout << "The text you introduced was:\n" << content;return 0;
}
//清空有效字符
//void clear();
int main()
{char c;std::string str;std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";do {c = std::cin.get();str += c;if (c == '\n'){std::cout << str;str.clear();}} while (c != '.');return 0;
}
//为字符串预留空间**
//void reserve (size_type n = 0);
int main()
{std::string str;std::ifstream file("test.txt", std::ios::in | std::ios::ate);if (file) {std::ifstream::streampos filesize = file.tellg();str.reserve(filesize);file.seekg(0);while (!file.eof()){str += file.get();}std::cout << str;}return 0;
}
//将有效字符的个数改成n个,多出的空间用字符C填充
//void resize (size_type n);
//void resize(size_type n, charT c)
int main()
{std::string str("I like to code in C");std::cout << str << '\n';std::string::size_type sz = str.size();str.resize(sz + 2, '+');std::cout << str << '\n';str.resize(14);std::cout << str << '\n';return 0;
}

三、类对象的访问及遍历操作

//返回pos位置的字符,const string类对象调用
//reference operator[] (size_type pos);
//const_reference operator[] (size_type pos) const;
int main()
{std::string str("Test string");for (int i = 0; i < str.length(); ++i){std::cout << str[i];}return 0;
}

四、string类对象的修改操作

//在字符串后面追加字符串str
//string(1)
//basic_string& operator+= (const basic_string& str);
//c - string(2)
//basic_string & operator+= (const charT * s);
//character(3)
//basic_string& operator+= (charT c);
int main()
{std::string name("John");std::string family("Smith");name += "K";         // c-stringname += family;         // stringname += '\n';           // characterstd::cout << name;return 0;
}
//返回C格式字符串
//const charT* c_str() const;
int main()
{std::string str("Please split this sentence into tokens");char* cstr = new char[str.length() + 1];std::strcpy(cstr, str.c_str());// cstr now contains a c-string copy of strchar* p = std::strtok(cstr, " ");while (p != 0){std::cout << p << '\n';p = strtok(NULL, " ");}delete[] cstr;return 0;
}
//从字符串pos位置开始往后找字符C,返回该字符在字符串中的位置
//string(1)
//size_type find(const basic_string& str, size_type pos = 0) const;
//c - string(2)
//size_type find(const charT * s, size_type pos = 0) const;
//buffer(3)
//size_type find(const charT* s, size_type pos, size_type n) const;
//character(4)
//size_type find(charT c, size_type pos = 0) const;
int main()
{std::string str("There are two needles in this haystack with needles.");std::string str2("needle");// different member versions of find in the same order as above:std::string::size_type found = str.find(str2);if (found != std::string::npos)std::cout << "first 'needle' found at: " << found << '\n';found = str.find("needles are small", found + 1, 6);if (found != std::string::npos)std::cout << "second 'needle' found at: " << found << '\n';found = str.find("haystack");if (found != std::string::npos)std::cout << "'haystack' also found at: " << found << '\n';found = str.find('.');if (found != std::string::npos)std::cout << "Period found at: " << found << '\n';// let's replace the first needle:str.replace(str.find(str2), str2.length(), "preposition");std::cout << str << '\n';return 0;
}

五、string类非成员函数

//输入运算符重载
//template 
//basic_istream& operator>> (basic_istream& is,
//	basic_string& str);
int main()
{std::string name;std::cout << "Please, enter your name: ";std::cin >> name;std::cout << "Hello, " << name << "!\n";return 0;
}
//输出运算符重载
//template 
//basic_ostream& operator<< (basic_ostream& os,
//	const basic_string& str);
int main()
{std::string str = "Hello world!";std::cout << str << '\n';return 0;
}
//获取一行字符串
//template 
//(1)basic_istream& getline(basic_istream& is,
//	basic_string& str, charT delim);
//(2)
//template 
//basic_istream& getline(basic_istream& is,
//	basic_string& str);
int main()
{std::string name;std::cout << "Please, enter your full name: ";std::getline(std::cin, name);std::cout << "Hello, " << name << "!\n";return 0;
}
//大小比较
int main()
{std::string foo = "alpha";std::string bar = "beta";if (foo == bar) std::cout << "foo and bar are equal\n";if (foo != bar) std::cout << "foo and bar are not equal\n";if (foo < bar) std::cout << "foo is less than bar\n";if (foo > bar) std::cout << "foo is greater than bar\n";if (foo <= bar) std::cout << "foo is less than or equal to bar\n";if (foo >= bar) std::cout << "foo is greater than or equal to bar\n";return 0;
}

六、汇总代码

#include
using namespace std;
#include
//测试string容量相关的接口
//size/clear/resize
void Teststring1()
{//注意:string类对象支持直接用cin和cout进行输入和输出string s("hello,sunlang!!!");//用C-siring来构造string类对象cout << s.size() << endl;//返回字符串有效字符长度cout << s.length() << endl;//返回字符串有效字符长度cout << s.capacity() << endl;//返回空间总大小cout << s << endl;//将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小s.clear();cout << s.size() << endl;cout << s.capacity() << endl;//将s中有效字符个数增加到10个,多出位置用‘a'进行补充//"aaaaaaaa"s.resize(10, 'a');cout << s.size() << endl;cout << s << endl;cout << s.capacity() << endl;//将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行补充//"aaaaaaaa\0\0\0\0"s.resize(15);cout << s.size() << endl;cout << s.capacity() << endl;cout << s << endl;//将s中有效字符个数缩小到5个s.resize(5);cout << s.size() << endl;cout << s.capacity() << endl;cout << s << endl;
}
void Teststring2()
{string s;//测试reserve是否会改变string中有效元素个数s.reserve(100);cout << s.size() << endl;cout << s.capacity() << endl;//测试reserve参数小于string的底层空间大小时,是否会将空间缩小s.reserve(50);cout << s.size() << endl;cout<

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...