实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
2017-01-06 23:55
741 查看
有时候也会遇到std:vector与转std:string 相互转换的情况。
首先看一下
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
string 转vector就会更容易:
================================================================
vector to stringstream
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
首先看一下
vector<char>如何转string:
std::vector<char> *data = response->getResponseData(); std::string res; //方法一 for (int i = 0;i<data->size();++i) { res+=(*data)[i]; } res+='\0'; std:cout << res; //方法二 std::vector<char> *data = response->getResponseData(); std::string res; res.insert(res.begin(), data->begin(), data->end()); std::cout << res; //方法三 std::vector<char> *data = response->getResponseData(); std::string res; const char* s = &(*data->begin()); res = std::string(s, data->size()); std::cout << res; //方法四 string ch = "what a fucking day!"; vector <char> ta; ta.resize(ch.size()); ta.assign(ch.begin(),ch.end());1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
string 转vector就会更容易:
vector <char> ta = {‘a’, 'b', 'c'}; ch.clear(); ch.assign(ta.begin(),ta.end());
================================================================
vector to stringstream
#include <iostream> #include <sstream> #include <vector> #include <string> #include <iterator> // Dummy std::vector of strings std::vector<std::string> sentence; sentence.push_back("aa"); sentence.push_back("ab"); // Required std::stringstream object std::stringstream ss; // Populate std::copy(sentence.begin(), sentence.end(),std::ostream_iterator<std::string>(ss,"\n")); // Display std::cout<<ss.str()<<std::endl;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
相关文章推荐
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- 实战c++中的vector系列--使用sort算法对vector<unique_ptr<string>>进行排序(sort函数出错“应输入 2 个参数,却提供了 3 个)
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- C++编译出现binding ‘const string {aka const std::__cxx11::basic_string<char>}’ to reference of type ‘std
- std::vector<char>* buffer 转换为 char*
- 实战c++中的vector系列--使用sort算法对vector<unique_ptr<string>>进行排序(sort函数“应输入 2 个参数,却提供了 3 个)
- std::vector<char> *buffer 转换为 char*
- 实战c++中的vector系列--vector<unique_ptr<>>初始化(所有权转移)
- C++ 类型转换 atoi atol atof <stdlib.h>, itoa ftoa char <---> string
- vector<string>转换为char*[]
- 实战c++中的vector系列--vector<unique_ptr<>>作为函数的参数
- C++ 不能通过‘...’传递有不能平凡复制的类型‘const string {aka const class std::basic_string<char>}’