У меня есть vector из pair по имени vec1. Как (самый быстрый) способ записать это в текстовый файл (в Linux)?
#include <iostream>
#include <utility>
#include <vector>
#include <fstream>
#include <iomanip>
int main() {
std::vector<std::pair<int, std::vector<float>>> vec1 { {1,{0.11,0.12,0.13}},
{2,{0.14,0.15,0.16}}, {3,{0.17,0.18,0.19}} };
}
Я пытался что-то вроде этого:
std::ofstream fout("file.txt");
fout << std::setprecision(4);
for(auto const& x : vec1)
fout << x << '\n';
но я получаю сообщение об ошибке:
error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’





Для std::pair<T, U> нет встроенного оператора вставки. Вы можете сделать его самостоятельно или распечатать поля вручную:
for (auto const& x : vec1) {
fout << x.first << ": ";
for (float f : x.second) fout << f << " ";
fout << '\n';
}