Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Vector<int> to String [duplicate]

Tags:

c++

string

vector

I want to make a program that first input array of string, then convert it into integer, and then push it to a vector.

The code is like this :

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);
}

The output :

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

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

I really appreciate any helps. Thanks.

like image 297
dulipat Avatar asked Aug 07 '13 09:08

dulipat


People also ask

How do I copy a vector to a string?

Using std::accumulate Another option to convert a vector to a string is using the standard function std::accumulate , defined in the header <numeric> . We can overwrite its default operation by providing a binary predicate to perform the concat operation on two strings and return the result.

How do you return a vector string in C++?

Returning a Vector Pointervector<string> *v = fn(&store); respectively. Note the presence and position of * in the return type of the function definition. Note the presence and position of & in the function call statement; it is in front of the argument, store, and not in front of fn(), which does not have & or *.

How does stoi work in C++?

In C++, the stoi() function converts a string to an integer value. The function is shorthand for “string to integer,” and C++ programmers use it to parse integers out of strings. The stoi() function is relatively new, as it was only added to the language as of its latest revision (C++11) in 2011.


2 Answers

[...] my question will be how to do that? To convert the vector [104 97 104 97] back to string "haha".

That's very easy. You can loop through std::vector elements, and use std::string::operator+= overload to concatenate the characters (whose ASCII values are stored in the std::vector) in the resulting string.

e.g.

#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

Just a small note on your original code:

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

You may want to use C++-style casts instead of old C-style casts in your code (i.e. static_cast in the above code).

Moreover, since you know the size of the vector, you should also know that valid indexes go from 0 to (size-1), so using simple fast and efficient std::vector::operator[] overload is just fine, instead of using the std::vector::at() method (with its index bounds-checking overhead).

So, I'd change your code like this:

x = static_cast<int>( a[i] );
like image 100
Mr.C64 Avatar answered Sep 30 '22 19:09

Mr.C64


 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;

}
like image 29
khajvah Avatar answered Sep 30 '22 18:09

khajvah