• STL 中 string 的使用


    赋值

    string 类型变量可以直接赋值

    str = "string"; // str 是 一个 string 类型变量 //等价于 str.assign("string");
    str = b; // b 可以是 string 类型 也可以是 char * 类型
    

    也可以方便地添加字符串

    str += 'c';
    str += "string";
    str += b;//b 可以是 string 类型 也可以是 char * 类型
    

    读取

    可以直接使用[]操作符读取。

    char ch = str[5]; // ch 值为 'g' // string str = "string";
    

    迭代器

    begin() 返回指向第一个字符的迭代器
    end() 返回指向最后一个字符之后的迭代器

    string 变量的字符范围[ begin(), end() )

    rbegin() 返回指向最后一个字符的迭代器
    rend() 返回指向第一个字符之前的迭代器 [ rbegin(), rend() )

    vector<char>::iterator itr = str.begin(); // string str = "string";
    

    容量

    size() 和 length() 都返回字符串的长度。

    clear() 清空字符串。
    empty() 检查字符串是否为空。

    str.size();
    str.length;
    str.clear();
    str.empty();
    

    修改

    push_back()、pop_back() 在字符串末尾 添加/删除 字符。
    swap() 交换两个字符串的内容。

    str.push_back('r'); //只能添加一个字符,不能添加字符串
    str.pop_back(); // 删除最后一个字符
    
    str.swap(str2);
    

    增加

    append

    append([字符串])							// 添加字符串
    append([字符串], [起始位置], [长度]) 	// 将 [字符串] 中从 [起始位置] 开始 [长度] 长的子串添加到字符串中
    append([个数], [字符]) 					// 添加 [个数] 个 [字符]
    
    str.append("this is a string");
    str.append("this is a string",5,2); // 添加了子串 "is"
    str.append(5,'c'); 						// 添加 "ccccc"
    

    insert

    insert([位置], [字符串])
    insert([位置], [字符串], [起始位置], [长度])
    insert([位置], [字符串], [长度])
    insert([位置], [个数], [字符])
    
    std::string str="to be question";
    std::string str2="the ";
    std::string str3="or not to be ";
    std::string::iterator it;
    
    str.insert(6,str2);                 // to be (the )question
    str.insert(6,str3,3,10);            // to be (not to be )the question
    str.insert(10,"that is cool",8);    // to be not to be (that is )the question
    str.insert(15,1,':');               // to be not to be (:) that is the question
    

    注意:插入的默认是字符串而不是字符,所以尝试在某个位置插入一个字符是不合法的。

    关于使用迭代器插入数据的方法未在此处列出。

    删除

    erase([位置]) 							// 删除从 [位置] 开始的子串
    erase([位置],[长度])						// 删除从 [位置] 开始 [长度] 长的子串
    
    erase([指针位置])							// 删除 [指针位置] 处的字符
    erase([指针起始位置], [结束位置])			// 删除 [ [指针位置起始位置], [结束位置] ) 范围内的子串
    
    str.erase(5);
    str.erase(5,3);
    
    str.erase(str.begin());
    str.erase(str.begin()+5,str.end());
    

    根据上述用法可知,当删除时已知删除长度,那么使用的位置是下标;当删除时知道的时开始位置和结束位置,那么使用的位置是迭代器。

    相关函数:clear() 见容量函数

    格式转换

    c_str() 转换为 char * 格式。

    str.c_str();
    

    查找

    find() 查找某个字符串或者某个字符,返回找到的第一个字符串的起始位置。

    str.find("find a char", 5);//从第五个字符开始找 “find a char”
    

    比较

    可以直接使用比较运算符比较两个 string 变量

    str == str2; //等价于 str.compare(str2); compare()函数返回int类型的比较结果。
    str > str2;
    

    string类型同样支持使用

    输入输出

    getline() 从输入流中获取一行字符串。

    std::getline(cin, str);
    

    参考代码

    #include <iostream>	// cin cout
    #include <string>	// string
    #include <vector>	// vector
    #include <fstream>	// ifstream
    
    using namespace std;
    
    int main(){
    	
    	//////////////////////////////////////////////////
    	//	赋值
    	
    	string a = "this is a string";
    	cout << a << endl;//this is a string
    	
    	//	直接给 string 类型变量赋值
    	a = "assign a string directly";
    	cout << a << endl;//assign a string directly
    	
    	//	通过 char * 类型变量直接赋值
    	char b[30] = "this is a char string";
    	a = b;
    	cout << a << endl;//this is a char string
    	
    	//////////////////////////////////////////////////
    	//	读取
    	
    	
    	cout << a[10] << endl;//c
    	
    	
    	//////////////////////////////////////////////////
    	//	迭代器
    	
    	
    	//	顺序遍历
    	for (vector<char>::iterator iterator = a.begin(); iterator < a.end(); iterator++) {
    		cout << *iterator;
    	}
    	cout << endl;
    	
    	//	逆序遍历
    	for (vector<char>::reverse_iterator iterator = a.rbegin(); iterator < a.rend(); iterator++) {
    		cout << *iterator;
    	}
    	cout << endl;
    	
    	
    	//////////////////////////////////////////////////
    	//	查找
    	
    	// 查找 “is”
    	std::size_t found = a.find("is");
    	if (found!=std::string::npos)
    		std::cout << "first 'is' found at: " << found << '
    ';
    	
    	// 查找下一个 “is”
    	found = a.find("is",found+1);
    	if (found!=std::string::npos)
    		std::cout << "second 'is' found at: " << found << '
    ';
    	
    	//////////////////////////////////////////////////
    	//	比较
    	
    	
    	a = "apple";
    	string c = "banana";
    
    	cout << (a == c) << endl;//0
    	cout << (a < c) << endl;//1
    	cout << a.compare("banana") << endl;// -1   "apple" < "banana"   
    	
    	
    	//////////////////////////////////////////////////
    	// 输入输出
    	
    	ifstream cin("/Users/apple/Applications/gyp/test/actions-multiple/src/input.txt");
    	
    	getline(cin,a);// 读取第一行
    	getline(cin,a);// 读取第二行
    	cout << a << endl;
    
    }
    
    
    



    知识共享许可协议
    作者:tangyikejun
    本文采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

  • 相关阅读:
    Python学习笔记(三)
    Python学习笔记(二)
    GDUFE ACM1159
    GDUEFE ACM1003 练手
    GDUFE ACM1033
    GDUFE ACM1128
    GDUFE ACM1002
    EDUFE ACM1050
    GDUFE ACM1007
    GDUFE ACM1003
  • 原文地址:https://www.cnblogs.com/tangyikejun/p/4198515.html
Copyright © 2020-2023  润新知