简体   繁体   English

C ++持久性矢量,用文本文件中的数据填充矢量

[英]C++ Persistent Vector, fill vector with data from a text file

i am currently trying to learn some C++ and now i got stuck in an exercise with vectors. 我目前正在尝试学习一些C ++,现在我陷入了向量练习中。 So the task is to read ints from a text file and store them in the vector which should be dynamic. 因此,任务是从文本文件中读取int并将其存储在应为动态的向量中。

I guess there is something wrong with the while-loop? 我猜while循环有问题吗? If I start this, the program fails and if I set the vector size to 6, I get 6 0 0 0 0 0 as output. 如果启动此程序,则程序将失败,并且如果将向量大小设置为6,则会得到6 0 0 0 0 0作为输出。

Thanks for any hints. 感谢您的任何提示。

int main()
{
const string filename = "test.txt";
int s = 0;
fstream f;
f.open(filename, ios::in);
vector<int> v;

if (f){
    while(f >> s){
        int i = 0;
        v[i] = s;
        i = i+1;
    }

  f.close();
}

for(int i = 0; i < 6; i++){
    cout << v[i] << "\n";
}

} }

  1. You don't grow the vector. 你不增加载体。 It is empty and cannot hold any int s. 它为空,不能容纳任何int You'll need to either resize it every time you want to add another int or you use push_back which automatically enlarges the vector. 您每次需要添加另一个int时都需要调整其大小,或者使用push_back自动放大向量。

  2. You set i = 0 for every iteration so you would change the first value of the vector every iteration instead of the next one. 您为每个迭代设置i = 0 ,因此您将在每个迭代中更改向量的第一个值,而不是下一个。

Go for: 去做:

    v.push_back(s);

in your loop and 在你的循环中

for(int i = 0; i < v.size(); i++) { // ...

Remark: 备注:

You normally don't hardcode vector sizes/bounds. 通常,您不对矢量大小/边界进行硬编码。 One major point about using std::vector is its ability to behave dynamically with respect to its size. 使用std::vector一个主要要点是它可以根据其大小动态地表现。 Thus, the code dealing with vectors should not impose any restrictions about the size of the vector onto the respective object. 因此,处理矢量的代码不应将矢量的大小强加到各个对象上。

Example: 例:

for(int i = 0; i < 6; i++){ cout << v[i] << "\\n"; }

requires the vector to have at least 6 elements, otherwise (less than 6 ints) you access values out of bounds (and you potentially miss elements if v contains more than 6 values). 要求向量至少包含6个元素,否则(小于6个整数)您将访问超出范围的值(如果v包含6个以上的值,则可能会丢失元素)。

Use either 使用任一

for(int i = 0; i < v.size(); i++){ cout << v[i] << "\\n"; }

or 要么

for(std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i) { cout << *i << "\\n"; }

or 要么

for(auto i = v.begin(); i != v.end(); ++i) { cout << *i << "\\n"; }

or 要么

for(int x : v){ cout << x << "\\n"; }

or 要么

for(auto && x : v){ cout << x << "\\n"; }

or 要么

std::for_each(v.begin(), v.end(), [](int x){ std::cout << x << "\\n"; });

or variants of the above which possibly pre-store v.size() or v.end() 或以上版本的变体,可能会预先存储v.size()v.end()

or whatever you like as long as you don't impose any restriction on the dynamic size of your vector. 或任何您喜欢的东西,只要您不对向量的动态大小施加任何限制。

Error is this line int i = 0; 错误是此行int i = 0; because you declare i=0 every time in while-loop. 因为您每次在while循环中都声明i = 0。 To correct this move this line outside from loop. 要更正此问题,请将此行从循环中移出。

Note: this will work, if you declare v like normal array for example int v[101] When you use std vectors you can just push element at the end of vector with v.push_back(element); 注意:如果您像普通数组那样声明v,例如int v[101]可以使用std vectors ,只需使用v.push_back(element);将元素推到向量的末尾v.push_back(element);

The issue is in the line i= 0 . 问题在i= 0 Fixing that will give an issue in the line v[i] = s . 解决该问题会在v[i] = s行中产生问题。

You always initialise i to 0 in the while loop, and that is responsible for the current output. 您总是在while循环中将i初始化为0 ,这负责当前输出。 You should shift it out of the while loop. 您应该将其移出while循环。

After fixing that, you have not allocated memory to that vector, and so v[i] doesn't make sense as it would access memory beyond bounds. 修复该问题之后,您尚未为该向量分配内存,因此v[i]毫无意义,因为它将超出范围访问内存。 This will give a segmentation fault. 这将产生分段错误。 Instead, it should be v.push_back(i) , as that adds elements to the end of a vector, and also allocates memory if needed. 相反,它应该是v.push_back(i) ,因为这会将元素添加到向量的末尾,并在需要时分配内存。

如果您使用的是std::vector ,则可以使用v.push_back(i)填充此向量

v[i] = s; //error,you dont malloc room for vector

更改为: v.push_back(s);

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

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