Re: vector of (int,string). can't convert i->first to int
Hi!
Milan Krejci schrieb:
i have a vector of 15,"something" ... 19,"something"...20,"something
else".. 32,"something" and i'm trying to find out from what int to what
int there is "something" in the pair. in other words i need to get
first==15, latest==19, typ=="something" and
first= , latest==31, typ=="something else".
Maybe this can help you. It is slightly different, but probably the
difference is no matter. It takes a map and reorders all elements (not
just adjacent) by the string. Then one can query all "int"s that
correspond to a string:
#include <iostream>
#include <ostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
std::pair<std::string, int> swapElements(
std::pair<int, std::string> const& what)
{
return std::make_pair(what.second, what.first);
}
void printElement(std::pair<std::string, int> const& what)
{
std::cout << what.first << ": " << what.second << '\n';
}
void foo()
{
using namespace std;
map<int, string> input;
multimap<string, int> result;
transform(
input.begin(), input.end(),
inserter(result, result.begin()),
&swapElements
);
//now query result:
for_each(result.begin(), result.end(), &printElement);
}
HTH,
Frank