so I got a simple struct like this:

struct scores
{
    std::vector<double> max_score,min_score;
    std::vector<string> names;
};

scores sc;

then I create: the vector sc.names containing several strings and do the same for the max/min_scores containing the scores.


Now, I just can't figure out how to write to a file all 3 vectors side by side (columns) like this:

name1 max_score1 min_score1

name2 max_score2 min_score2

...

std::ofstream testwrite;
testwrite.open("test.txt");

testwrite << std::setprecision(4);
testwrite << std::fixed;
for (std::vector<double>::iterator it = sc.max_score.begin();it != sc.max_score.end();++it)
{
    testwrite << *it << "\n";
}

testwrite.close();
有帮助吗?

解决方案

if you're sure that the vectors are of the same length, you can just iterate over the indices:

for (int i=0; i < sc.max_score.length(); ++i)
  testwrite << sc.names[i] << " " << sc.max_score[i] << ... << std::endl;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top