我正在编写一个程序,它将城市名称的文本文件读入向量,然后使用 stl::map 将每个城市与提升循环缓冲区相关联。我还有一个温度数据向量,在将其作为字符串从另一个文本文件中读取后,我将其转换为双精度类型。我想知道如何将这些数据输入我选择的循环缓冲区。例如,温度数据来自波士顿,所以我想将其放入与波士顿相关的循环缓冲区中。如果有人能告诉我如何做到这一点,我将不胜感激!这是我的代码。与地图有关的代码在底部附近。
#include < map >
#include < algorithm >
#include < cstdlib >
#include < fstream >
#include < iostream >
#include < iterator >
#include < stdexcept >
#include < string >
#include < sstream >
#include < vector >
#include < utility >
#include < boost/circular_buffer.hpp >
double StrToDouble(std::string const& s) // a function to convert string vectors to double.
{
std::istringstream iss(s);
double value;
if (!(iss >> value)) throw std::runtime_error("invalid double");
return value;
}
using namespace std;
int main()
{
std::fstream fileone("tempdata.txt"); // reading the temperature data into a vector.
std::string x;
vector<string> datastring (0);
while (getline(fileone, x))
{
datastring.push_back(x);
}
vector<double>datadouble;
std::transform(datastring.begin(), datastring.end(), std::back_inserter(datadouble), StrToDouble); // converting it to double using the function
std::fstream filetwo("cities.txt"); // reading the cities into a vector.
std::string y;
vector<string> cities (0);
while (getline(filetwo, y))
{
cities.push_back(y);
}
map<string,boost::circular_buffer<double>*> cities_and_temps; // creating a map to associate each city with a circular buffer.
for (unsigned int i = 0; i < cities.size(); i++)
{
cities_and_temps.insert(make_pair(cities.at(i), new boost::circular_buffer<double>(32)));
}
return 0;
}