c++:访问字符串映射的元素和 Boost 循环缓冲区

发布于 2024-11-03 04:33:36 字数 1828 浏览 0 评论 0原文

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

I'm writing a program that reads a text file of city names into a vector, then uses a stl::map to associate each city with a boost circular buffer. I also have a vector of temperature data, which I converted to double type after reading it as strings from another text file. I want to know how to feed this data into a circular buffer of my choosing. For example, the temperature data is from Boston, so I want to put it into the circular buffer associated with Boston. If someone could show me how to do this I would really appreciate it! Here's my code. The code having to do with maps is near the bottom.

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-11-10 04:33:36

您可以使用一对迭代器初始化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())));

You can initialize a circular_buffer with a pair of iterators, like so:

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

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

I'm not exactly sure how you want to apply this in your specific case. You only have one vector of doubles, but I don't know how many cities. If you wanted to insert that entire datum into the circular buffer, the way you have it, it would be like this:

cities_and_temps.insert(make_pair(
    cities.at(i),
    new boost::circular_buffer<double>(datadouble.begin(), datadouble.end())));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文