在Vector中,一个向量包含8000个成员
Struct in Vector, a vector include 8000 members
我在构建8000行向量时遇到了一些问题。每行是一个包含5列的结构。我不确定C++有什么没有响应甚至错误消息。。。它只是说"线程‘Win32线程’(0x3b48)已退出,代码为-103741510(0xc000013a)。线程"Win32线程"(0x309c)已退出,代码为-103741510(0xc000013a)。程序"[13048]Branch Filter Vector.exe:Native"已退出,代码为-103741510(0xc000013a)。"
我的代码将是
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <vector>
using namespace std;
struct branch {
long int FromBusNum;
string FromBusName;
double FromBusVoltage;
long int ToBusNum;
string ToBusName;
;
};
int main()
{
vector<branch> myBranch(8000);
ifstream infile;
long int x1;
string x2;
double x3;
long int x4;
string x5;
;
int num = 0; // num must start at 0
//infile.open("Data.txt");
if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
return 1; // no point continuing if the file didn't open...
}
string dummyLine; //do not read in the first line
getline(infile, dummyLine);
while(!infile.eof()) // reads file to end of *file*, not line
{
myBranch.push_back(branch());
infile>>x1 >> x2 >> x3 >> x4
>> x5 ;
myBranch[num].FromBusNum = x1;
myBranch[num].FromBusName = x2;
myBranch[num].FromBusVoltage = x3;
myBranch[num].ToBusNum = x4;
myBranch[num].ToBusName = x5;
++num; // go to the next number
}
infile.close();
ofstream fout("valency.txt");
for(int i=0;i<num;i++)
fout/*<<myBranch[i].FromBusNum<<"t"
<<myBranch[i].FromBusName<<endl;
fout.close();
system("pause");
return 0; // everything went right.
}
不确定问题出现在哪里。。。提前谢谢!
发布的代码有一些"损坏的部分"。我首先修复了"不要在循环中推回",作为常见的"这就是你应该做的",将infile >> x1 >> ...
移动到while条件中。这有几个好处:
- 它不会多次运行循环(因为您读取了最后一整行,然后再运行一次迭代,因为只有当代码试图读取文件末尾的PAST时才会检测到EOF)
- 如果数据未能正确读取(例如,某个应该是数字的东西中有字母),代码将停止读取数据。这可能不是一个奇妙的解决方案,但它比"永远循环"要好,如果你只检测到EOF,就会出现这种情况,因为当试图读取错误的东西时,处理不会进行——它只是在那一点上停止读取,所有其他输入值都会被跳过。这往往会带来一个无休止的循环,因为不再发生阅读,也永远达不到EOF。我怀疑这就是注释中达到20000+数字的原因——输入文本中存在某种错误(例如,字符串中的空格使下一个输入不同步)
- 它有点短/不那么复杂(节省代码空间)。这确实是一件小事,但无论如何都不是缺点
我只做了足够的工作,使它能够正常工作,所以代码中可能仍然存在一些小问题。例如,从文件中读取数据应该比这更彻底地检查错误,例如,读取整行,然后将其拆分,并对每个条目进行错误检查(与上面的第2点有关,我在写下这句话后写的)。如果你想知道一个文件中有多少行,你可能还想检查num
是否没有克服这一点,如果克服了,就会出错。
这是我想出的代码:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <vector>
using namespace std;
struct branch {
long int FromBusNum;
string FromBusName;
double FromBusVoltage;
long int ToBusNum;
string ToBusName;
};
int main()
{
vector<branch> myBranch(8000);
ifstream infile;
long int x1;
string x2;
double x3;
long int x4;
string x5;
int num = 0; // num must start at 0
infile.open("Data.txt");
if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
return 1; // no point continuing if the file didn't open...
}
string dummyLine; //do not read in the first line
getline(infile, dummyLine);
while(infile>>x1 >> x2 >> x3 >> x4 >> x5) // reads file to end of *file*, not line
{
myBranch[num].FromBusNum = x1;
myBranch[num].FromBusName = x2;
myBranch[num].FromBusVoltage = x3;
myBranch[num].ToBusNum = x4;
myBranch[num].ToBusName = x5;
++num; // go to the next number
}
infile.close();
ofstream fout("data.out");
if (fout.fail())
{
cout << "Error on outfile" << endl;
return 2;
}
for(auto v : myBranch)
{
fout << v.FromBusNum << " "
<< v.FromBusName << " "
<< v.FromBusVoltage << " "
<< v.ToBusNum << " "
<< v.ToBusName << endl;
}
cout << "num=" << num << endl;
return 0; // everything went right.
}
我运行的数据可以在这里找到:https://gist.github.com/Leporacanthicus/1c25dd0f9b00d090f1a5
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- C++从另一个类访问公共静态向量的正确方法是什么
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 获取向量C++中第一个值和最后一个值的和
- 从多个源构造一个对象,包括一个对象向量
- 为什么一个向量上的多线程操作很慢
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 将一个向量插入另一个向量的某个位置
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 将指向给定子类的指针从一个向量复制到另一个向量
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何在不复制的情况下将一个向量移动到另一个向量中
- 创建一个函数的 Python 绑定,返回指向带有 boost 的向量的指针
- 紧凑向量一个小的内存足迹向量