1

我正在编写一个程序,它将城市名称的文本文件读入向量,然后使用 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;
}
4

1 回答 1

1

您可以使用一对迭代器初始化一个 circular_buffer,如下所示:

std::vector<double> v;
...
... // Fill v with data
...

boost::circular_buffer<double> cb(v.begin(), v.end());

我不确定你想如何在你的具体情况下应用它。你只有一个双打向量,但我不知道有多少个城市。如果您想将整个数据插入循环缓冲区,按照您的方式,它将是这样的:

cities_and_temps.insert(make_pair(
    cities.at(i),
    new boost::circular_buffer<double>(datadouble.begin(), datadouble.end())));
于 2011-04-23T18:19:39.773 回答