Convert Vector <int> to String

I want to create a program that first enters an array of a string, then converts it to an integer, and then clicks on a vector.

The code is as follows:

string a; vector<long long int> c; cout << "Enter the message = "; cin >> a; cout << endl; cout << "Converted Message to integer = "; for (i=0;i<a.size();i++) { x=(int)a.at(i); cout << x << " "; //convert every element string to integer c.push_back(x); } 

Exit:

 Enter the message = haha Converted Message to integer = 104 97 104 97 

Then I write it in a file, and in the next program I want to read it and convert it back to a string, my question will be, how to do it? To convert the vector [104 97 104 97] back to the string "haha".

I really appreciate any help. Thanks.

+4
source share
5 answers
  std::vector<int> data = {104, 97, 104, 97}; std::string actualword; char ch; for (int i = 0; i < data.size(); i++) { ch = data[i]; actualword += ch; } 
+4
source

[...] my question will be how to do this? To convert the vector [104 97 104 97] back to the string β€œhaha”.

It is very simple. You can scroll through the std::vector elements and use the std::string::operator+= overload to concatenate characters (whose ASCII values ​​are stored in std::vector ) in the resulting string.

eg.

 #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<int> v = {104, 97, 104, 97}; string s; for (auto x : v) { s += static_cast<char>(x); } cout << s << endl; } 

Console output:

 C:\TEMP\CppTests>g++ test.cpp C:\TEMP\CppTests>a.exe haha 

A quick note about your source code:

x = (integer) a.at (i);

You can use the C ++ style instead of the old C-style styles in your code (i.e. static_cast in the code above).

Also, since you know the size of the vector, you should also know that real indices go from 0 to (size-1) , so using the simple fast and efficient overload of std::vector::operator[] just fine, instead of using std::vector::at() (with its service index labels).

So, I would modify your code as follows:

 x = static_cast<int>( a[i] ); 
+6
source
 #include <algorithm> #include <iostream> int main() { std::vector<int> v = { 104, 97, 104, 97 }; std::string res(v.size(), 0); std::transform(v.begin(), v.end(), res.begin(), [](int k) { return static_cast<char>(k); }); std::cout << res << '\n'; return 0; } 

Two notes:

  • It would be very desirable to change the vector to std::vector<char> , which will facilitate the task, and static_cast<char>(k) potentially dangerous.
  • Always avoid C-style styles. If you really need to, use reinterpret_cast , but in your case a static_cast would do the trick too. C-style cast does a lot of bad things, such as implicit const casting or selling your soul.
+3
source

Use the iterator constructor std::string :

 std::vector<long long int> v{'h', 'a', 'h', 'a'}; //read from file std::string s{std::begin(v), std::end(v)}; std::cout << s; //or manipulate how you want 

He asks why your vector contains long long int when it should only store characters. Keep an eye on this while trying to convert it to a string.

+3
source

You can use std :: transform using your own function object or a lambda function that does the inverse transformation, i.e. (char) (int).

+1
source

Source: https://habr.com/ru/post/1495709/


All Articles