赋值

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 << '\n'; // 查找下一个 “is”
found = a.find("is",found+1);
if (found!=std::string::npos)
std::cout << "second 'is' found at: " << found << '\n'; //////////////////////////////////////////////////
// 比较 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; }

STL 中 string 的使用的更多相关文章

  1. 65)STL中string的知识

    1)代码展示: string是一个类,只不过封装了 char*  而且还封装了  很多的字符串操作函数 2)string类的初始化: string的构造函数 ²  默认构造函数: string();  ...

  2. STL中map用法

    Map是 STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于 这个特性,它完成有可能在我们处理一对一数据的 ...

  3. (转载) STL中map用法详解

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...

  4. c++中string (MFC)

    题目:UVALive - 6439    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...

  5. STL库中string类内存布局的探究

    在STL中有着一个类就是string类,他的内存布局和存储机制究竟是怎么样的呢? 这就是建立好的string 可以看出,图中用黄色框框标注的部分就是主要区域 我们用来给string对象进行初始化的字符 ...

  6. STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  7. 【转】 STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  8. C++之STL之string

    /*C 语言中字符数组一般会采用char str[]来存放,但是显得会比较麻烦,C++在stl中加入了string类型,对字符串常用的功能进行了封装,操作起来比较方便*/#include<cst ...

  9. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...

随机推荐

  1. vue/cli新旧版本安装方式

    一.老版本安装  Shift+鼠标右键 选择打开命令窗口 1.创建项目之前,需先确保本机已经安装node 在命令窗口中执行node -v npm -v 2.一般情况下用npm安装东西比较慢,可以使用淘 ...

  2. 1001 害死人不偿命的(3n+1)猜想 (15 分)

    卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 (3n+1) 砍掉一半.这样一直反复砍下去,最后一定在某一步得到 n=1.卡拉兹在 1950 ...

  3. 文件上传报错:Unknown: file created in the system's temporary directory

    nginx+php下文件上传成功,但会有错误提示如下: <b>Notice</b>:  Unknown: file created in the system's tempor ...

  4. C lang: Compound literal

    Xx_Introduction C99 stantard. Upate array and struct a compound literal. Literal is date type value. ...

  5. python中 遇到的读取坑2.7和3.6版本的问题

    2.7读取,需要使用io.open 3.x使用open 使用io.open的时候路径需要使用\\ 目前io.open的文件名不能为中文

  6. MySQL删除大表时潜在的问题(drop table,truncate table)

    来源于:https://www.cnblogs.com/CtripDBA/p/11465315.html,侵删,纯截图,避免吸引流量之嫌 case1,删除大表时,因为清理自适应hash索引占用的内容导 ...

  7. 小程序模板template使用介绍

    template(模板):是可以在wxml中引用的代码,就是在wxml中引用公用的wxml类型的代码,它的作用类似于组件,因此这里简单的说明下template与Component (组件)的区别. t ...

  8. MySQL 学习笔记 (一)

    1.InnoDB and Online DDL ALTER TABLE tbl_name ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE; ...

  9. Django使用MySQL数据库的流程

    Django使用MySQL数据库的流程 手动创建一个MySQL数据库 配置数据库 ENGINE MySQL NAME 数据库的名字 HOST ip PORT 3306 USER 用户名 PASSWOR ...

  10. Struts2.0

    流程详解:   配置详解:   constant  (常用常量配置) 使用Struts2 框架 ,先要导入Struts2 需要的jar 包 , 通过配置中央控制器 以及web.xml 来实现  Str ...