1

都市名のテキスト ファイルをベクトルに読み取り、stl::map を使用して各都市をブースト循環バッファーに関連付けるプログラムを作成しています。また、別のテキスト ファイルから文字列として読み取った後、double 型に変換した温度データのベクトルもあります。このデータを選択した循環バッファにフィードする方法を知りたいです。たとえば、温度データはボストンのものなので、ボストンに関連付けられた循環バッファーに入れたいと考えています。誰かがこれを行う方法を教えてくれたら、本当に感謝しています! これが私のコードです。マップに関係するコードは一番下にあります。

#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());

特定のケースでこれをどのように適用したいのか正確にはわかりません。double のベクトルは 1 つしかありませんが、都市の数はわかりません。そのデータム全体を循環バッファーに挿入したい場合は、次のようになります。

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