我有一个C++代码来从csv文件加载2d矢量。这里,output.csv文件的数据为65 x 74496。在这段代码中,我将行和列的大小分别固定为65和74496。但是,在将来,行和列的数量可能会根据数据进行更改。你知道从csv文件中自动选择行数和列数的方法吗。谢谢
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
const std::string csvFileName{ "output.csv" };
constexpr size_t NumberOfRows = 65U; // I want to decide this number from csv file automatically
constexpr size_t NumberOfColumns = 74496U; // I want to decide this number from csv file automatically
int main() {
// writeTestFile();
// Our data
std::vector<std::vector<std::string>> data(NumberOfRows, std::vector<std::string>(NumberOfColumns));
// Open file and check, if it is open
if (std::ifstream testFileStream{ csvFileName }){//; testFileStream) {
size_t row{};
std::string line{};
line.resize(1'000'000U);
// Read all lines/rows in a loop
while (testFileStream >> line) {
size_t columnIndex{};
std::vector<std::string> columns(NumberOfColumns);
// split strings
std::istringstream iss{ line };
for (std::string part{}; std::getline(iss, part, ','); columns[columnIndex++] = std::move(part))
;
// Add all column values
data[row++] = std::move(columns);
}
}
else std::cerr << "nnError: Could not open file: " << csvFileName << "nn";
std::cout << "Hello World!n";
std::cout << data[1][1] << std::endl;
return 0;
}
查看此答案
这里是使用动态数组大小的修改版本。
对于46MB的sam源文件,它需要1399ms。
ANd写了好几次:请在编译器中启用C++17!!!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
const std::string csvFileName{ "r:\test.csv" };
constexpr size_t NumberOfRows = 65U;
constexpr size_t NumberOfColumns = 74496U;
void writeTestFile() {
// Open file and check, if it could be opened
if (std::ofstream testFileStream{ csvFileName }; testFileStream) {
// Write all columns
for (size_t row{}; row < NumberOfRows; ++row) {
// Special handling for separator. No comma at the end
bool writeCommaFirst{ false };
// For all columns
for (size_t col{}; col < NumberOfColumns; ++col) {
// // Create some test string
std::string temp = "R" + std::to_string(row) + "C" + std::to_string(col);
// Write data
testFileStream << (writeCommaFirst ? "," : "") << temp;
writeCommaFirst = true;
}
testFileStream << 'n';
}
}
else std::cerr << "nnError. Could not write file" << csvFileName << "nn";
}
int main() {
// writeTestFile();
// Our data
std::vector<std::vector<std::string>> data{};
// Open file and check, if it is open
if (std::ifstream testFileStream{ csvFileName }; testFileStream) {
std::string line{};
line.reserve(1'000'000U);
// Read all lines/rows in a loop
while (std::getline(testFileStream, line)) {
size_t columnIndex{};
std::vector<std::string> columns{};
// split strings
std::istringstream iss{ line };
for (std::string part{}; std::getline(iss, part, ','); columns.emplace_back(std::move(part)))
;
// Add all column values
data.emplace_back(std::move(columns));
}
}
else std::cerr << "nnError: Could not open file: " << csvFileName << "nn";
return 0;
}