gpt4 book ai didi

c++ - 如何在嵌套循环中匹配来自两个字符串 vector 的字符串值

转载 作者:太空宇宙 更新时间:2023-11-04 13:58:53 25 4
gpt4 key购买 nike

我正在尝试在 CSV 文件中查找某些 header 值的索引,这样我就可以使用它们来提取文件其余部分中这些位置的数据。我将 header 值添加到 map<std::string, int> 中这样我就可以保留索引。

我有工作代码,直到我注意到如果标题是行中的最后一个值它不匹配。最后一个 header 字符串在我的嵌套循环中是空的,但在外循环中不是。

const int columnCount = 2;
std::string columns[columnCount] = { "column1", "column2" };

map<std::string, int> columnMap;

std::vector<std::string> cols(columns, columns + columnCount);
std::vector<std::string> cells;

boost::tokenizer<boost::escaped_list_separator<char> > tok(header_row);
cells.assign(tok.begin(), tok.end());

std::vector<std::string>::iterator iter_cells;
std::vector<std::string>::iterator iter_cols;

for (iter_cells = cells.begin(); iter_cells != cells.end(); ++iter_cells) {
std::string cell = *iter_cells;
for(iter_cols = cols.begin(); iter_cols != cols.end(); ++iter_cols) {
std::string col = *iter_cols;
cout << cell << "=" << col;
if(col.compare(cell) == 0) {
cout << " MATCH" << endl;
columnMap.insert(std::make_pair(*iter_cols,iter_cells-cells.begin()));
break;
}
cout << endl;
}
}

哪里tok(row)相当于tok("column0,column1,column2")我得到了这个输出;

column0=column1
column0=column2
column1=column1 MATCH
=column1
=column2

而如果它是 tok("column0,column1,column2,column3")我明白了;

column0=column1
column0=column2
column1=column1 MATCH
column2=column1
column2=column2 MATCH
=column1
=column2

当我 cout << cell在外循环中,值显示正确。

为什么我丢失了 cell 的值在内循环中?

编辑

Code in github和测试文件编译;

gcc parse_csv.cpp -o parse_csv -lboost_filesystem -lmysqlpp

并执行

./parse_csv /home/dave/SO_Q/

我得到了这个输出;

Process File: /home/dave/SO_Q/test_2.csv
metTime
metTime=metTime MATCH
Ta
=metTime
=Ta
=Ua
=Th
Process File: /home/dave/SO_Q/test_1.csv
DATE_TIME_UTC
DATE_TIME_UTC=metTime
DATE_TIME_UTC=Ta
DATE_TIME_UTC=Ua
DATE_TIME_UTC=Th
Ta
Ta=metTime
Ta=Ta MATCH
metTime
=metTime
=TaTime
=UaTime
=ThTime

最佳答案

不确定你是如何填充变量“header_row”的,但下面的代码对我有用,我得到了这个输出

column0=column1

column0=column2

column1=column1 MATCH

column2=column1

column2=column2 MATCH

column3=column1

column3=column2

#include  <boost/tokenizer.hpp>
#include <iostream>
#include <fstream>
#include <map>

using namespace std;



int main()
{
//create csv
ofstream csvFile ("data.csv");
std::string row = "column0,column1,column2,column3";

csvFile << row;

csvFile.close();

const int columnCount = 2;
std::string columns[columnCount] = { "column1", "column2" };

map<std::string, int> columnMap;

std::vector<std::string> cols(columns, columns + columnCount);
std::vector<std::string> cells;

//open csv file
std::string header_row;
ifstream csvRead("data.csv");
assert(csvRead.is_open());
getline(csvRead,header_row);

boost::tokenizer<boost::escaped_list_separator<char> > tok(header_row);
cells.assign(tok.begin(), tok.end());
//close file
csvRead.close();

std::vector<std::string>::iterator iter_cells;
std::vector<std::string>::iterator iter_cols;

//original loops as provided in question
for (iter_cells = cells.begin(); iter_cells != cells.end(); ++iter_cells) {
std::string cell = *iter_cells;
for(iter_cols = cols.begin(); iter_cols != cols.end(); ++iter_cols) {
std::string col = *iter_cols;
cout << cell << "=" << col;
if(col.compare(cell) == 0) {
cout << " MATCH" << endl;
columnMap.insert(std::make_pair(*iter_cols,iter_cells-cells.begin()));
break;
}
cout << endl;
}
}

}

关于c++ - 如何在嵌套循环中匹配来自两个字符串 vector 的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20138787/

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