简体   繁体   中英

Fast C++ I/O to read a string (char array) into a vector, plus resolving error

Right now I am trying to use a vector of charpointers to hold a bunch of individual strings

vector<char*> strings(10);

The individual string is of form char indString[100]

And I am doing this to assign each one

for (int i=0; i < iLimit; ++i){
    gets(indString);
    strings[i] = indString;
}

But when I output everything

for (auto & stri: strings) cout << stri << endl;

I just get a bunch of repeats of the last string read.

You are just storing the pointer to the buffer used to read... all the pointers will point to the same buffer and this is why you see the last string repeated.

Just use an std::vector<std::string> instead.

You are adding the same pointer to the vector at every iteration which is causing you to see duplicates in the output. You can do this efficiently and cleanly using std::getline , std::string and std::move .

Below is a working example that reads strings from cin and outputs the contents of the vector.

#include <vector>
#include <string>

void readtext(
    std::istream& input,
    std::vector<std::string>& text,
    const size_t limit)
{
    std::string line;
    for (size_t count = 0; count < limit && std::getline(input, line); ++count)
        text.push_back(std::move(line));
}

int main()
{
    std::vector<std::string> text;
    readtext(std::cin, text, 3);
    for (auto& line : text)
        std::cout << line << std::endl;
}

They all are pointing to the same pointer. I'd suggest doing this:

char** PointerArray = new char*[NumOfStrings];

//input strings

for( int i = 0; i < NumOfStrings; i++ )
     cout << PointerArray[i];

for( int i = 0; i < NumOfStrings; i++ )
     delete[] PointerArray[i];

EDIT: If you require vectors, make an array of character arrays, then pass each character into it.

OR you could try this:

char** PointerArray = new char*[NumOfStrings];

vector<char*> pointerArray(NumOfStrings);
for( int i = 0; i < NumOfStrings; i++ )
     pointerArray[i] = PointerArray[i];
//stuff
for( int i = 0; i < NumOfStrings; i++ )
     delete[] pointerArray[i];

You are pointing all your strings[i] to the same location... you need to allocate different memory locations for each string, and read them into that location. With char arrays, it would look like this:

char* strings[10];

for (int i=0; i < iLimit; ++i){
    strings[i] = malloc(sizeOfLargestString + 1);
    gets(strings[i]);
}

Or full code (tested):

#include <stdio.h>
#include <stdlib.h>
#define sizeOfLargestString 100
#define N 3

int main(void) {
  int ii;
  char* strings[N];
  for (ii=0; ii < N; ii++) {
    strings[ii] = (char*)malloc(sizeOfLargestString + 1);
    gets(strings[ii]);
  }
  for(ii=0 ;ii< N; ii++) {
    printf("%s\n", strings[ii]);
  }
  return 0;
}

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