This question already has answers here: How to read a file into vector in C++? (5个答案) 7个月前关闭。 我不明白如何将一个外部文件中的某种序列(例如数字序列)分配给一个vector / array。如果只是几个数字,则只需将它们写在方括号中。如果您有100000个号码怎么办?我希望有人有足够的资格回答这个问题。我需要编写一个脚本,该脚本将求和数组的特定成员。正在从一个文件中提取数组,然后将其放入另一个文件中。但是,我更需要它成为vector,但我不明白它是如何工作的。在分配值并以某种方式初始化vector之后,我还需要指定vector成员是数字,然后将它们加起来。在main中添加过程。这两个for循环应搜索向量的特定成员。所以第一个周期为f = 0,我应该加上:x = 1 / AC *(A *(评分员[第4名成员] +评分员[4 + 3 =第7名成员] +评分员[4 + 6 =第10名成员] +评分员[4 + 9 =第13名成员])+ C *评分员[4-3 =第一名成员])然后将其打印在文件+“”字段分隔符中,然后x = 1 / AC *(A *(评分员[第5名成员] +评分员[5 + 3 =第8名成员] +评分员[5 + 6 =第11名成员] +评分员[5 + 9 =第14名成员])+ C *评分者[5-3 =第二名成员])最后x = 1 / AC *(A *(评分员[6th成员] +评分者[6 + 3 = 9th成员] +评分者[6 + 6 = 12th成员] +评分者[6 + 9 = 15th成员])+ C *评分者[6-3 =第三名成员])cout 那么第一个for循环应该做同样的事情,只是f应该是15个成员,像这样:f = 15,所以i = 15 + 4 = 19x = 1 / AC *(A *(评分员[第19名成员] +评分员[19 + 3 =第22名成员] +评分员[4 + 6 =第10名成员] +评分员[4 + 9 =第13名成员])+ C *评分者[4-3 =第一名成员])等等...附言我不显示输入文件,因为提取过程有效。已经有人帮助了我。希望代码和数组就足够了。#include <iostream>#include <fstream>#include <string>#include <vector>#include <cmath>#include <set>using namespace std;const double A = 2.66E-23, B = 4.65E-23, AC = 1.53E-22, c = 0;const int SIZE = 360;double grades[SIZE];void readData(){ ifstream ifs; ofstream ofs; ifstream inFile("out.txt", std::ifstream::in); ofstream of("out1.txt", std::ofstream::out); streambuf* origBuf = cout.rdbuf(); cout.rdbuf(of.rdbuf()); //inFile.open(inFileName.c_str()); if (inFile.is_open()) { std::string s; // to read 2 letters like Ti in your input after edition for (int i = 0; i < SIZE; i++) { if ((i % 3) == 0) { if (!(inFile >> s)) { std::cerr << std::endl << "error when reading letter(s) line " << (i / 3) + 1 << std::endl; return; } } if (!(inFile >> grades[i])) { std::cerr << std::endl << "error when reading the " << i + 1 << " nth double" << std::endl; return; } std::cout << grades[i] << " "; } cout << endl; // std::cout << grades[i] << " "; } else { //Error message std::cerr << "Can't find input file " << inFile << std::endl; } cout.rdbuf(origBuf); ifs.close(); ofs.close(); return;}int main(){ readData(); std::vector<int> grader[SIZE]; double x; int f = SIZE; for (f = 0; f < SIZE; f += 15) { for (int i = f + 4; i < i + 3; i++) { x = 1 / AC * (A * (grader[i] + grader[i + 3] + grader[i + 6] + grader[i + 9]) + C * grader[i - 3]); cout << x << " "; } } return 0;}我想成为vector的数组:3.07097 5.31908 5.50539 -3.37566 6.15709 4.24421 4.97241 6.00196 6.76656 -1.53792 5.17956 5.60283 4.9001 3.92165 5.40794 3.07098 4.6307 1.83512 3.64437 3.94783 0.573947 3.37567 3.7927 3.0963 3.71667 6.02814 1.93257 1.53791 4.77024 1.7377 1.374.16.243.72.87.682.7243.72.879.24.374.79.374.79.374.79.374.79.374.79.374.79.374.79.374.79.374.79.374.79.24.79.24.79.374.07.24.79.374.79.24.79.243.792.374.79.374.79.24.79.243.792.374.79.374.79.24.79.24.07.374.37.24.79.243.792.37.374.37.24.37.24.378.943.36.76.37.39.243.724.39.24.37.24.37.78.37.45 3.37567 1.18219 0.573947 3.64437 1.02706 3.0963 1.53791 0.204662 1.93257 -0.591718 6.4091 1.7377 6.14195 0 5.50539 2.71164 7.30721 4.24421 7.02003 0.155135 6.76656 0.946195 6.20444 5.60283 5.25459 1.25791 5.40794 6.78321 2.48745 1.83512 5.90513 2.332315.7577.8 4.81976 18.9252 -1.59675 5.13003 21.4476 5.25459 3.71699 20.2838 -3.36219 6.2328 20.089 2.47482 4.9749 9.17565 1.59674 5.13003 10.4368 1.59674 4.81976 7.91448 3.36219 6.2328 9.0782 3.36219 3.71699 9.27307 1.23741 7.11816 20.1864 0.664017 6.43529 18 .9252 0.932718 6.28015 21.4476 4.9001 1.05324 20.2838 2.77047 7.25768 20.089 -1.23742 2.83164 9.17565 -0.664026 3.51451 10.4368 -0.932727 3.66965 7.91448 -0.591714 1.4342 9.0782 5.8463 2.69211 9.27307 1.23741 2.83164 20.1864 0.932718 3.66965 18.9252 2.4721.7694 0.932727 6.28015 10.4368 -0.664026 6.43529 7.91448 -2.77047 7.25768 9.0782 3.71668 1.05324 9.27307 5.5458 0.344186 12.8459 4.97241 1.02706 14.1071 5.24111 1.18219 11.5847 0.591709 6.4091 12.7485 7.07886 0.204662 12.9433 5.5458 4.6307 12.8459 2.4724.74.724 1.4721.74.724 1.4721.74.52.424 1.8721.74.52.824 1.8721.74.52.424 1.4721.74.52.824 14.1071 2.71165 2.33231 11.5847 0.946195 3.74536 12.7485 0.946199 1.22954 12.9433 2.47482 0 16.5162 -2.71164 7.30721 15.255 1.59674 0.155135 17.7773 -0.946203 6.20444 16.6136 3.36219 1.25791 16.4187 3.07097 5.31908 16.5162 3.37566 6.15709 15.255 3.3 1.5777437 91 5.17956 16.6136 3.71668 3.92165 16.4187 -1.23741 2.14326 16.5162 -0.664021 1.46039 15.255 7.68405 1.30525 17.7773 -0.591718 3.5407 16.6136 5.8463 2.28278 16.4187 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 这是错的std::vector<int> grader[SIZE];那是向量的数组,我确定这不是你想要的尝试这个std::vector<int> grader(SIZE);这是大小为SIZE的向量。以正确的方式声明向量后,就可以像使用数组一样使用它。这是错的for (f = 0; f < SIZE; f += 15){ for (int i = f + 4; i < i + 3; i++) { x = 1 / AC * (A * (grader[i] + grader[i + 3] + grader[i + 6] + grader[i + 9]) + C * grader[i - 3]); cout << x << " "; }}当f等于SIZE - 1时,i等于f + 4或SIZE + 3,因此grader[i]将是越界访问。无论grader是向量还是数组,这都是正确的。这是错的for (int i = f + 4; i < i + 3; i++)i < i + 3始终为true。因此,这显然不是您想要的。向量很容易,您的错误要么在语法上,要么与向量完全无关。 (adsbygoogle = window.adsbygoogle || []).push({});