Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70018ee

Browse files
string_from_vector_of_char.cpp
1 parent 6bfe4ed commit 70018ee

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

string_from_vector_of_char.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
//https://stackoverflow.com/questions/37022167/copy-null-terminated-char-array-to-stdstring-respecting-buffer-length
9+
10+
int main()
11+
{
12+
const char arr[100] = "hello world";
13+
const vector<char> vc(arr, arr+sizeof(arr));
14+
15+
str = string(vc.begin(), vc.end());
16+
cout << "last char: " << str.back() << endl;
17+
18+
vector<char>::const_iterator it = find(vc.begin(), vc.end(), '\0');
19+
string str(vc.begin(), it);
20+
cout << "last char: " << str.back() << endl;
21+
return 0;
22+
}
23+
24+
/**
25+
last char: d
26+
last char:
27+
**/

0 commit comments

Comments
 (0)