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