简体   繁体   中英

expression must have a constant value

I am trying to create an array out of a string input.

string input;
getline(cin,input);
string inputarray1[100];
istringstream pp(input);
int* inputPosition=0;
while (!pp.eof())
{
    getline( pp, inputarray1[*inputPosition], ' ' );
    inputPosition++;
}
int* a = inputPosition;
string halp[a];

I am using getline to parse my input (along with an istringstream) and placing that into an array, but how can I create an array that has no extra empty locations?

Use a vector, from the header <vector>

vector<string> inputArray;
while (getline(pp, input, ' '))
    inputArray.push_back(input);

The number of strings can be obtained with inputArray.size() , and you can access individual elements just like with an array, inputArray[index] .

Note that operator>> is delimited on whitespace, so you can probably also do this(unless you for some reason want to treat tabs differently)

while (pp >> input)
    inputArray.push_back(input);

I'm not 100% clear on your question, but it sounds like you really want to use a Hash Table instead of an array. This will let you map user inputs to something else, without the empty array locations you mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM