string容器c++
时间: 2023-11-24 15:48:59 浏览: 42
string容器是C++ STL中的一个类,用于处理字符串。它提供了许多方法来操作字符串,例如插入、删除、截取、查找等。string容器可以使用构造函数来创建一个空的字符串,也可以使用字符串或字符数组来初始化。在使用string容器时,我们可以使用下标运算符或迭代器来访问字符串中的字符。此外,string容器还提供了许多其他有用的方法,例如比较、替换、大小写转换等。总之,string容器是一个非常方便和实用的工具,可以帮助我们更轻松地处理字符串。
相关问题
c++sort string
C++中的sort函数可以用来对给定区间的元素进行排序。在C++中,使用sort函数对字符串数组进行排序的方法如下所示:
1. 引入头文件和命名空间:
```cpp
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
```
2. 编写排序函数:
```cpp
void sort_string(string* in_array, int n, string* out_array) {
vector<string> strArray;
for (int i = 0; i < n; i++) {
strArray.push_back(in_array[i]);
}
sort(strArray.begin(), strArray.end());
vector<string>::iterator st;
int j = 0;
for (st = strArray.begin(); st != strArray.end(); st++) {
out_array[j] = *st;
j++;
}
}
```
3. 调用排序函数并输出结果:
```cpp
int main() {
string str[4] = { "hello, world!", "welcome to cpp.", "effective c++", "exceptional c++" };
string str_out[4];
sort_string(str, 4, str_out);
for (int j = 0; j < 4; j++) {
cout << str_out[j] << endl;
}
}
```
以上代码中,首先将字符串数组转换为vector容器,然后使用sort函数对容器进行排序,最后将排序后的结果存入输出数组中并输出。
c++vector用法string
C++中的vector是一个动态数组容器,可以存储不同类型的元素。而string是C++中的字符串类,用于处理字符串操作。
使用vector时,需要包含头文件< vector >。下面是vector的一些常见用法:
1. 创建vector对象:
```
vector<int> vec; // 创建一个空的整型向量
vector<string> strVec(5); // 创建一个包含5个空字符串的字符串向量
vector<double> doubleVec(10, 3.14); // 创建一个包含10个初始值为3.14的双精度浮点数向量
```
2. 向vector中添加元素:
```
vec.push_back(10); // 在向量末尾添加一个元素
vec.insert(vec.begin(), 5); // 在向量开头插入一个元素
```
3. 访问vector中的元素:
```
int num = vec[0]; // 通过下标访问元素
int size = vec.size(); // 获取向量的大小
```
4. 遍历vector中的元素:
```
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
```
使用string时,需要包含头文件< string >。下面是string的一些常见用法:
1. 创建string对象:
```
string str; // 创建一个空字符串
string greeting = "Hello"; // 创建一个包含初始值的字符串
```
2. 连接字符串:
```
string fullName = firstName + " " + lastName; // 使用+运算符连接字符串
fullName.append(" Jr."); // 在字符串末尾添加另一个字符串
```
3. 获取字符串长度:
```
int length = str.length(); // 获取字符串的长度
```
4. 截取子串:
```
string subStr = str.substr(startIndex, length); // 从指定位置开始截取指定长度的子串
```
5. 查找子串:
```
size_t found = str.find("world"); // 查找子串在字符串中的位置
if (found != string::npos) {
cout << "子串找到了!" << endl;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)