gpt4 book ai didi

C++ : Adding rows to a vector of vectors

转载 作者:行者123 更新时间:2023-11-30 05:09:06 25 4
gpt4 key购买 nike

我试图在 C++ 中模仿 R 中的一些数据框功能,即从 CSV 文件读取到矩阵并添加/删除行。 CSV 文件中的行数可以是任意的,但列数及其数据类型是固定的。所以它不应该太通用(即列的可变数量或列的可变数据类型)。我已经能够制作一个基本程序,将数据读取到字符串 vector 的 vector 中。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

using vec = vector<string>;
using matrix = vector<vec>;


matrix readCSV(string filename)
{
char separator = ',';
matrix result;
string row, item;

ifstream in(filename);
while(getline(in, row))
{
vec R;
stringstream ss(row);
while(getline(ss, item, separator)) R.push_back( item );
result.push_back(R);
}
in.close();
return result;
}



void printMatrix(const matrix &M)
{
for(vec row : M)
{
for (string s:row) cout << setw( 12 ) << left << s << " ";
cout << '\n';
}
}



void deleteRow(matrix &M, int row)
{
if(row < M.size()) M.erase( M.begin() + row );
}



void deleteCol(matrix &M, int col)
{
for(vec &row : M) if ( col < row.size() ) row.erase( row.begin() + col );
}



void edit( matrix &M, int i, int j, string value )
{
if (i < M.size() && j < M[i].size()) M[i][j] = value;
}



int main()
{
matrix pets = readCSV( "pets.csv" );
printMatrix( pets );

cout << "\n\n";

deleteRow( pets, 3 );
deleteCol( pets, 3 );
edit( pets, 1, 2, "12" );
printMatrix( pets );
}

宠物.csv:

Animal,Name,Age,Food,Owner,Notes
Dog,Fido,6,Chewies,R. Smith,Barks
Cat,Fluffy,8,Whiskers,M. Jones,Miaows
Hamster,Giles,2,Scratchies,A. Green
Snake,Hissie,3,Mice,Bob

输出:

Animal       Name         Age          Food         Owner        Notes        
Dog Fido 6 Chewies R. Smith Barks
Cat Fluffy 8 Whiskers M. Jones Miaows
Hamster Giles 2 Scratchies A. Green
Snake Hissie 3 Mice Bob


Animal Name Age Owner Notes
Dog Fido 12 R. Smith Barks
Cat Fluffy 8 M. Jones Miaows
Snake Hissie 3 Bob

主要问题是所有列都是相同的数据类型(在本例中为字符串)。我应该做哪些修改以允许不同数据类型的列(例如,在这种情况下,年龄列应该是 int 和其余的字符串)?另外,如何向矩阵添加新行或新列?

最佳答案

如果每一列都是不同的类型,这意味着您应该有一个对象的 vector ,而不是 vector 的 vector 。您处理的不是表格数据,而是记录

类似于:

struct Pet
{
std::string animal;
std::string name;
unsigned int age;
std::string food;
std::string owner;
std::string notes;
};

// Later
std::vector<Pet> pets;

在这个模型中:

how do I add new rows to the matrix?

将一个新的 Pet 对象插入 vector 中。

how do I add new columns to the matrix?

Pet 类型添加一个新的数据成员。

how can I read data from the stringstream into the struct object

使用辅助函数进行读取:

Pet read_pet(std::string const &row)
{
Pet pet;

std::istringstream ss{row};

std::getline(ss, pet.animal, ',');
std::getline(ss, pet.name, ',');
ss >> pet.age;
ss.ignore(std::numeric_limits<std::streamsize>::max(), ',');
std::getline(ss, pet.food, ',');
std::getline(ss, pet.owner, ',');
std::getline(ss, pet.notes, ',');

return pet;
}

and then push that into the matrix?

while(getline(in, row))
{
result.emplace_back(read_pet(row));
}

关于C++ : Adding rows to a vector of vectors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46353438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com