我正在编写一个从文件中获取值的程序。 该值需要在以后修改,所以我将它们存储在一些 vector 中。
基本上,程序所做的是:
- 使用 while 循环读取整个文件。
- 如果一行包含必需的参数数组,则使用 for 循环将数组中的每个值存储在一个 vector 中。
问题是:
for 循环一退出,vector 就失去了一切。 我在读取文件的 while 循环之外声明了我的 vector ,所以这不应该发生。 有人知道这是怎么回事吗?
示例文件:
<polylist count="34">
<input semantic="VERTEX" source="#Cylinder-mesh-vertices" offset="0"/>
<input semantic="NORMAL" source="#Cylinder-mesh-normals" offset="1"/>
<vcount>4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 32 4 4 32 </vcount>
<p>0 0 1 0 3 0 2 0 2 1 3 1 5 1 4 1 4 2 5 2 7 2 6 2 6 3 7 3 9 3 8 3 8 4 9 4 11 4 10 4 10 5 11 5 13 5 12 5 12 6 13 6 15 6 14 6 14 7 15 7 17 7 16 7 16 8 17 8 19 8 18 8 18 9 19 9 21 9 20 9 20 10 21 10 23 10 22 10 22 11 23 11 25 11 24 11 24 12 25 12 27 12 26 12 26 13 27 13 29 13 28 13 28 14 29 14 31 14 30 14 30 15 31 15 33 15 32 15 32 16 33 16 35 16 34 16 34 17 35 17 37 17 36 17 36 18 37 18 39 18 38 18 38 19 39 19 41 19 40 19 40 20 41 20 43 20 42 20 42 21 43 21 45 21 44 21 44 22 45 22 47 22 46 22 46 23 47 23 49 23 48 23 48 24 49 24 51 24 50 24 50 25 51 25 53 25 52 25 52 26 53 26 55 26 54 26 54 27 55 27 57 27 56 27 56 28 57 28 59 28 58 28 58 29 59 29 61 29 60 29 3 30 1 30 63 30 61 30 59 30 57 30 55 30 53 30 51 30 49 30 47 30 45 30 43 30 41 30 39 30 37 30 35 30 33 30 31 30 29 30 27 30 25 30 23 30 21 30 19 30 17 30 15 30 13 30 11 30 9 30 7 30 5 30 62 31 63 31 1 31 0 31 60 32 61 32 63 32 62 32 0 33 2 33 4 33 6 33 8 33 10 33 12 33 14 33 16 33 18 33 20 33 22 33 24 33 26 33 28 33 30 33 32 33 34 33 36 33 38 33 40 33 42 33 44 33 46 33 48 33 50 33 52 33 54 33 56 33 58 33 60 33 62 33</p>
</polylist>
C++代码:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
int main(){
vector<int> meshPrimLengthArray;
ifstream readCollada("myCollada.dae", ios::in);
string currentLine("");
while(getline(readCollada, currentLine)){ //loop that read the file
if(currentLine.find("<polylist") != std::string::npos){ //the given line tell us on many value the next array will contain, so I can reserve the required space in my vector.
string numberOfVcount(currentLine.substr(currentLine.find("count=\"")+7, currentLine.find("\">")-(currentLine.find("count=\"")+7)));
meshPrimLengthArray.reserve(atoi(numberOfVcount.c_str()));
}
else if((currentLine.find("<vcount>") != std::string::npos) && (currentLine.find("</vcount>") != std::string::npos)){
currentLine = currentLine.substr(currentLine.find("<vcount>")+8, currentLine.find("</vcount>")-(currentLine.find("<vcount>")+8));
istringstream vcountArray(currentLine);
if(vcountArray){
for(int i(0); i<(int)meshPrimLengthArray.capacity(); i++){
vcountArray >> meshPrimLengthArray[i];
cout << " " << meshPrimLengthArray[i] << ","; //show that the value have been inserted into the vector.
}
cout << endl << "meshPrimLengthArray.size() = " << meshPrimLengthArray.size() << endl; //show that for some unknow reason the vector have been cleared.
}
}
}
cout << endl << "Press Enter to continu" << endl;
cin.get();
cin.ignore();
return 0;
}
最佳答案
vcountArray >> meshPrimLengthArray[i]
期望元素已经被构造,但它们不是。 std::vector::reserve
不会那样做, std::vector::resize
做。您的代码表现出未定义的行为。
您也可以只使用临时的和 std::vector::push_back
(与 std::vector::reserve
一起使用,这使得它可能更快 - 避免重新分配)。
最后,查看 std::vector::size
之间的区别和 std::vector::capacity
.
如果您使用 operator[]
, 使用 i < meshPrimLengthArray.size()
作为循环条件。
更喜欢使用正确的类型 - size_t
在这种情况下,而不是 int
和 c 风格的转换!
当然,这个循环可以翻译成 Range-based for loop .
关于C++ vector 不保留从循环接收的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35165618/