如何将(int)字符串存储到int数组中

时间:2015-05-12 21:17:54

标签: c++ arrays string ascii

int ascii[1000] = {0};
string *data = (string*)malloc ( 1000*sizeof( string));
char *text = (char*)malloc ( 1000 *sizeof( char));
cout << "Enter the first arrangement of data." << endl;
cin.getline(text, 1000);
char *token = strtok(text, " ");

while ( token != NULL )
{
    if ( strlen(token) > 0)
    {
        cout << "The tokens are: " << token << endl;
        data[Tcount++] = *token;

    }
    token = strtok(NULL, " ");

    for(i=0; i < (Tcount); i++)
      {
          ascii[i] = (int)data[i]; // error here
      }

我使用此代码构建解析器,并且我想存储存储在&#39;数据&#39;中的令牌的ascii值。到一个名为&#39; ascii&#39;的数组中   当我运行程序时,我收到错误消息,&#34;错误:分配给&#39; int&#39;来自不兼容的类型&#39;字符串&#39; (又名&#39; basic_string,allocator&gt;&#39;)
任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

One thing before the main event here. Obviously you're allowed to use std::string so, let's get the data in a more civilized fashion.

if (comp instanceof Composite) { 
    //do something
}

we now have all of the individual words on the line packed into a nice resizable container, a vector. No messy dynamic memory to clean up.

Step 2: turn those strings into ints. 'Fraid you can't do that, ace. You could take a string that represents a number and turn it into an int. That's easy. Dozens of ways to do it. I like strtol.

But the ascii values are character by character. A string is a variable number of characters. You can pack one into an int, shift the int over by the width of one character and stuff in another, but you're going to run out of space after probably 4 or 8 characters.

Let's go with that, shall we? And we'll do it the old way without an iterator.

std::vector<std::string> data;
std::string line;
std::getline(cin, line); //read a whole line
std::stringstream tokenizer(line); // stuff the line into an object that's 
                                   // really good at tokenizing
std::string token;
while (tokenizer >> token) // one by one push a word out of the tokenizer 
{
    data.push_back(token); //and stuff it into a vector
}

Done. Not very useful unless all the strings are pretty short, but done.

Instead if you're going to do a parser why not go full geek and try this:

std::string data;
int ascii = 0;

if (data.length() > 0)
{
    ascii |= data[index];
    for(size_t index = 0; index < data.length(); index++)
    {
        ascii <<= 8; //we're talking ascii here so no unicode bit counting games 
        ascii |= data[index];
    }
}

Where somethingfunc is a function that looks like typedef void handlerfunc(); std::map<std::string, handlerfunc> parser; parser["do something"] = somethingfunc; parser["do something else"] = somethingelsefunc; that, obviously, does something. Dito somethingelsefunc. Only it does somethingelse.

Usage could be as simple as:

void somethingfunc()

But it's not. Sigh.

It's more like

parser[token]();

But seriously, look into some of the fun stuff a good container can do for you. Save a ton of time.

I crapped out all of the code without a compiler. Please let me know if I borked any of it.

相关问题