C++ - 用带分隔符的文件中的数据填充多维向量
C++ - Filling multidimensional vector with data from file with delimiters
我需要用带分隔符的文件中的数据填充我的多维向量,所以我这样做了:
ifstream file;
vector < vector<string> > stockVector;
vector<string> rowVector;
file.open( fileLocation(), std::ios::in | std::ios::out );
if ( !file.good() )
return false;
string data;
while ( getline( file, data, ';' ) ) {
for( int i = 0; i < 20; i++ ) {
rowVector.push_back(data);
}
stockVector.push_back( rowVector );
}
但我到处都是 1
。什么不好?
这是我的文件:
1;Guerra;36;0.95;
2;Rivas;14;3.20;
3;Petty;30;7.81;
4;Gallagher;65;8.00;
5;Nguyen;76;1.99;
6;Wooten;57;0.97;
7;Guerra;53;7.25;
8;Norman;6;5.82;
9;Dyer;72;8.69;
10;Martin;67;4.73;
11;Delgado;73;4.60;
12;Velasquez;87;1.76;
13;Crawford;95;0.66;
14;Houston;9;7.78;
15;Shaffer;85;4.40;
16;Hoover;99;0.81;
17;Webb;97;8.02;
18;Gonzales;22;1.78;
19;Gross;17;4.00;
20;Bridges;93;8.08;
这里有一些错误,包括一个细微的错误。
1) 这里需要两个 getline
:一个用于从文件中读取(惊喜)行直到 \n
,另一个用于从读取的行中提取标记。
2) 第二个getline
需要有一个分隔符;
.
3) 你的两个循环相互矛盾。 while
循环从文件中逐个标记地读取(错误地!)标记,另一个循环将相同的标记添加 20 次。
4)(微妙的错误)您没有为文件中的每一行重置 rowVector
。
5) 硬编码 20
是个坏主意,最好让 getline
为您找到文件和字符串的结尾。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <iterator>
int main(int argc, char **argv) {
std::ifstream file;
std::vector < std::vector<std::string> > stockVector;
file.open( argv[1], std::ios::in | std::ios::out );
if ( !file.good() )
return false;
for ( std::string data; getline(file, data); ) {
std::istringstream linestr(data);
std::vector<std::string> rowVector;
for( std::string token; std::getline(linestr, token, ';'); ) {
rowVector.push_back(token);
}
stockVector.push_back( rowVector );
}
for (const auto& v: stockVector ) {
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << "\n";
}
}
参考参考:
http://en.cppreference.com/w/cpp/string/basic_string/getline
http://en.cppreference.com/w/cpp/io/basic_stringstream
http://en.cppreference.com/w/cpp/iterator/ostream_iterator
我需要用带分隔符的文件中的数据填充我的多维向量,所以我这样做了:
ifstream file;
vector < vector<string> > stockVector;
vector<string> rowVector;
file.open( fileLocation(), std::ios::in | std::ios::out );
if ( !file.good() )
return false;
string data;
while ( getline( file, data, ';' ) ) {
for( int i = 0; i < 20; i++ ) {
rowVector.push_back(data);
}
stockVector.push_back( rowVector );
}
但我到处都是 1
。什么不好?
这是我的文件:
1;Guerra;36;0.95;
2;Rivas;14;3.20;
3;Petty;30;7.81;
4;Gallagher;65;8.00;
5;Nguyen;76;1.99;
6;Wooten;57;0.97;
7;Guerra;53;7.25;
8;Norman;6;5.82;
9;Dyer;72;8.69;
10;Martin;67;4.73;
11;Delgado;73;4.60;
12;Velasquez;87;1.76;
13;Crawford;95;0.66;
14;Houston;9;7.78;
15;Shaffer;85;4.40;
16;Hoover;99;0.81;
17;Webb;97;8.02;
18;Gonzales;22;1.78;
19;Gross;17;4.00;
20;Bridges;93;8.08;
这里有一些错误,包括一个细微的错误。
1) 这里需要两个 getline
:一个用于从文件中读取(惊喜)行直到 \n
,另一个用于从读取的行中提取标记。
2) 第二个getline
需要有一个分隔符;
.
3) 你的两个循环相互矛盾。 while
循环从文件中逐个标记地读取(错误地!)标记,另一个循环将相同的标记添加 20 次。
4)(微妙的错误)您没有为文件中的每一行重置 rowVector
。
5) 硬编码 20
是个坏主意,最好让 getline
为您找到文件和字符串的结尾。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <iterator>
int main(int argc, char **argv) {
std::ifstream file;
std::vector < std::vector<std::string> > stockVector;
file.open( argv[1], std::ios::in | std::ios::out );
if ( !file.good() )
return false;
for ( std::string data; getline(file, data); ) {
std::istringstream linestr(data);
std::vector<std::string> rowVector;
for( std::string token; std::getline(linestr, token, ';'); ) {
rowVector.push_back(token);
}
stockVector.push_back( rowVector );
}
for (const auto& v: stockVector ) {
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << "\n";
}
}
参考参考:
http://en.cppreference.com/w/cpp/string/basic_string/getline http://en.cppreference.com/w/cpp/io/basic_stringstream http://en.cppreference.com/w/cpp/iterator/ostream_iterator