简体   繁体   English

快速C ++ I / O将字符串(char数组)读入向量,加上解决错误

[英]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 现在我正在尝试使用charpointers向量来保存一堆单独的字符串

vector<char*> strings(10);

The individual string is of form char indString[100] 单个字符串的形式为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. 只需使用std::vector<std::string>

You are adding the same pointer to the vector at every iteration which is causing you to see duplicates in the output. 您在每次迭代时向vector添加相同的指针,这会导致您在输出中看到重复项。 You can do this efficiently and cleanly using std::getline , std::string and std::move . 你可以使用std::getlinestd::stringstd::move 有效干净地完成这项工作。

Below is a working example that reads strings from cin and outputs the contents of the vector. 下面是一个工作示例,它从cin读取字符串并输出向量的内容。

#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. 您将所有strings[i]指向同一位置...您需要为每个字符串分配不同的内存位置,并将它们读入该位置。 With char arrays, it would look like this: 使用char数组,它看起来像这样:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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