5

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.

1

5 Answers 5

6

[...] 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] );
5
 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;

}
3

Use std::string's iterator constructor:

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

It does beg the question why your vector contains long long int when it should only be storing characters, though. Watch out for that when trying to convert it to a string.

2
  • Will that work with vector of long long?
    – Neil Kirk
    Aug 7, 2013 at 10:31
  • @NeilKirk, As long as they're all within range, which they'd better be. A vector of characters makes more sense to me.
    – chris
    Aug 7, 2013 at 10:36
3
#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:

  1. It would be strongly advisable to change your vector to std::vector<char> - that would make this task easier, and static_cast<char>(k) is potentially dangerous.
  2. Avoid C-style casts at all times. If you really need to, use reinterpret_cast, but in your case a static_cast would have done the trick as well. C-style casts do a lot of bad things, like implicit const casting or selling your soul.
2
  • 1
    Implicit const casting IS selling your soul. Aug 7, 2013 at 10:03
  • @StoryTeller Yeah you're probably right, but I wasn't sure if everyone sees it that way. The one of us who sold their soul for a doughnut already may consider const casting as worse.
    – nikolas
    Aug 7, 2013 at 10:04
1

You could use std::transform using your own function object or lambda function making the reverse cast, i.e. (char)(int).

Not the answer you're looking for? Browse other questions tagged or ask your own question.