gpt4 book ai didi

c++ - 如何从 dat 文件生成 map 矩阵

转载 作者:行者123 更新时间:2023-11-28 03:00:41 24 4
gpt4 key购买 nike

我有一个 .dat 文件并想从中获取 map

   A  C  D  E  F  G  H  I  K  L  M  N  P  Q  R  S  T  V  W  Y
A 4 0 -2 -1 -2 0 -2 -1 -1 -1 -1 -2 -1 -1 -1 1 0 0 -3 -2
C 0 9 -3 -4 -2 -3 -3 -1 -3 -1 -1 -3 -3 -3 -3 -1 -1 -1 -2 -2
D -2 -3 6 2 -3 -1 -1 -3 -1 -4 -3 1 -1 0 -2 0 -1 -3 -4 -3
E -1 -4 2 5 -3 -2 0 -3 1 -3 -2 0 -1 2 0 0 -1 -2 -3 -2
F -2 -2 -3 -3 6 -3 -1 0 -3 0 0 -3 -4 -3 -3 -2 -2 -1 1 3
G 0 -3 -1 -2 -3 6 -2 -4 -2 -4 -3 0 -2 -2 -2 0 -2 -3 -2 -3
H -2 -3 -1 0 -1 -2 8 -3 -1 -3 -2 1 -2 0 0 -1 -2 -3 -2 2
I -1 -1 -3 -3 0 -4 -3 4 -3 2 1 -3 -3 -3 -3 -2 -1 3 -3 -1
K -1 -3 -1 1 -3 -2 -1 -3 5 -2 -1 0 -1 1 2 0 -1 -2 -3 -2
L -1 -1 -4 -3 0 -4 -3 2 -2 4 2 -3 -3 -2 -2 -2 -1 1 -2 -1
M -1 -1 -3 -2 0 -3 -2 1 -1 2 5 -2 -2 0 -1 -1 -1 1 -1 -1
N -2 -3 1 0 -3 0 1 -3 0 -3 -2 6 -2 0 0 1 0 -3 -4 -2
P -1 -3 -1 -1 -4 -2 -2 -3 -1 -3 -2 -2 7 -1 -2 -1 -1 -2 -4 -3
Q -1 -3 0 2 -3 -2 0 -3 1 -2 0 0 -1 5 1 0 -1 -2 -2 -1
R -1 -3 -2 0 -3 -2 0 -3 2 -2 -1 0 -2 1 5 -1 -1 -3 -3 -2
S 1 -1 0 0 -2 0 -1 -2 0 -2 -1 1 -1 0 -1 4 1 -2 -3 -2
T 0 -1 -1 -1 -2 -2 -2 -1 -1 -1 -1 0 -1 -1 -1 1 5 0 -2 -2
V 0 -1 -3 -2 -1 -3 -3 3 -2 1 1 -3 -2 -2 -3 -2 0 4 -3 -1
W -3 -2 -4 -3 1 -2 -2 -3 -3 -2 -1 -4 -4 -2 -3 -3 -2 -3 11 2
Y -2 -2 -3 -2 3 -3 2 -1 -2 -1 -1 -2 -3 -1 -2 -2 -2 -1 2 7

但是我在填充 map 时遇到了 for 循环的问题

static void gen_matrix(map<string, int>& mat)
{
const string myfile = "myfile.dat";
vector<string> myChars = { "A", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "Y" };
ifstream f(myfile);
string temporal;
getline(f, temporal); // Ignore first string
while (getline(f, temporal))
{
auto buff(split_string(temporal));
auto prefix = buff[0];
size_t i = 1;
//try 1
// for(auto it = myChars.begin(), end = myChars.end(); it != end; ++it){
// mat[prefix + it] = stoi(buff[i++]);
// }
//try 2
//for(auto& var = 0; myChars){
// mat[prefix + var] = stoi(buff[i++]);
//}
}
}

我想实现的是每个功能

for each (const auto& var in myChars)
{
mat[prefix + var] = stoi(buff[i++]);
}

但是没有成功,如何更改for循环?

最佳答案

由于您还没有显示您的split_string,如果我理解正确,您可以尝试以下操作:

std::vector<std::string> split(const std::string &s) {
std::stringstream ss(s);
std::string item;
std::vector<std::string> elems;
while (ss >> item) {
elems.push_back(item);
}
return elems;
}


std::string temporal;
std::getline(fin, temporal,'\r'); // You may need this carriage return as delim
std::map<std::string, int> mat;
std::vector<std::string> buff;

while(std::getline(f, temporal,'\r'))
{
buff = split( temporal) ;
for(std::size_t x=1; x < buff.size() ; ++x)
mat[buff[0]+myChars[x-1]] = atoi( buff[x].c_str() );
}

关于c++ - 如何从 dat 文件生成 map<string, int> 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20930619/

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