tags:

views:

647

answers:

5

What is the best way to convert a fixed length string array to a fixed lengh integer array in C++ ?

+10  A: 

This will copy an array of characters into an array of ints:

#include <algorithm>
char foo[9] = "asdfasdf";
int bar[9];
std::copy(foo, foo+9, bar);

std::copy

This assigns the values of a null terminated character array {'a', 's', 'd', 'f', 'a', 's', 'd', 'f', '\0'} to an integer array, yielding {97, 115, 100, 102, 97, 115, 100, 102, 0}. Note that this includes the null termination of the original string.


This will parse an array of strings, and put their integer values into an array of ints:

#include <algorithm>
#include <sstream>
#include <string>

template <class T>
T parse(const std::string& str)
{
    T temp;
    std::istringstream iss(str);
    iss >> temp;
    if(iss.bad() || iss.fail())
    {
        // handle conversion failure
    }
    return temp;
}

...

std::string foo[3];
int bar[3];
foo[0] = "67";
foo[1] = "11";
foo[2] = "42";

std::transform(foo, foo+3, bar, parse<int>);

std::transform

This will transform each of the strings in the array foo into ints, and place them in the array of ints, bar.

luke
I think he meant *string* array, not char array. Eg: char *foo[] = {"fred", "joe", "jim");
T.E.D.
Added a second method to cover that.
luke
Voting up, but personally would use atoi(char*) instead of the std::istringstream.
Max Lybbert
Why would you use atoi?
Eclipse
+6  A: 
#include <algorithm>

std::string foo[9];
int bar[9];

std::transform(foo, foo+9, bar, MyMagicStringToIntFunction);

Where MyMagicStringToIntFunction is whichever function you wish to use to convert your strings to integers. Since you didn't specify how you wanted that to be done, I can't answer that part.

That's my guess at what you want to do, but some more information would be helpful. (By "string array do you mean an array of std::strings? And how would you like to perform the conversion?"

In any case, std::transform is my best best, but you'll have to fill in the gaps yourself.

jalf
A: 

If you want to use vectors instead of arrays, this might give you some ideas.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <ostream>
#include <algorithm>
#include <sstream>
using namespace std;

int int_from_string(const string& s) {
    istringstream ss(s);
    int i;
    ss >> i;
    return i;
}

vector<int> int_vec_from_string_vec(const vector<string>& vstr) {
    vector<int> v(vstr.size());
    transform(vstr.begin(), vstr.end(), v.begin(), int_from_string);
    return v;
}

int main() {
    const vector<string> vstr(3, "45");
    const vector<int> vint = int_vec_from_string_vec(vstr);
    copy(vint.begin(), vint.end(), ostream_iterator<int>(cout, "\n"));
}
Shadow2531
+2  A: 

string array -> int array

Loop over the string array, and convert each string successively into the corresponging integer using std::istringstream

std::size_t const N = 3;
std::string a[N] = { "10", "-2", "5" };
int b[N];

for(std::size_t i = 0; i < N; i++) {
    std::istringstream sstream(a[i]);
    sstream >> b[i];
}

If you have many values, the constant recreation of the stringstream can introduce substantial overhead. You can take it out of the loop.

std::stringstream sstream;
for(std::size_t i = 0; i < N; i++) {
    sstream << a[i];
    sstream >> b[i];
    sstream.clear(); 
    sstream.seekp(0); sstream.seekg(0);
}

char array -> int array

If you want to convert a char array a into an int array b, you can do it like this:

std::size_t const N = 15;
char a[N] = { "this is a test" };
int b[N];

for(std::size_t i = 0; i < N; i++)
    b[i] = (int)(unsigned char) a[i];

The cast to unsigned char makes the values of the int array positive (useful if you want to convert extended 8 bit characters which will be represented in a signed 8bit char type as a negative number and would otherwise be negative integers too). If you don't want this behaivor, you can just omit that cast safely.

Johannes Schaub - litb
A: 

If you want to explode a byte array to an integer array, you can just do it char by char:

char foo[10];
int bar[10];

for(int i = 0; i < 10; ++i) {
  bar[i] = (int)foo[i];
}

But I'm guessing this isn't what you're looking for...

Sam Hoice