Re: vector of (int,string). can't convert i->first to int
Milan Krejci wrote:
Probably better not to top post here, someone might complain about it.
hello thanks for the reply.
i have a vector
Minor point, but you mean a map, not a vector, right?
of 15,"something" ... 19,"something"...20,"something
else".. 32,"something"
What do the ellipses represent? Are there other values?
15,"something"
16,"something"
17,"something"
18,"something"
19,"something"
20,"something else"
21,"something else"
and so on up to
31,"something else"
32,"something"
> and i'm trying to find out from what int to what
int there is "something" in the pair.
Do you really mean "something" or just any value? Could we see:
10,"X"
11,"X"
12,""
13,""
14,"Y"
15,"X"
16,"X"
in other words i need to get
first==15, latest==19, typ=="something" and
first= , latest==31, typ=="something else".
Given the data you provided, would you also see
> first==32, latest==32, typ=="something"
QString was a QT toolkit's implementation of a string class.
Why are you using it? I think it just confuses the issue. For one
thing you're using std::string in your map, for another, it's not part
of the C++ standard and at least some of the readers here are unfamiliar
with it.
Jim Langston napsal(a):
"Milan Krejci" <rdw@no-spam.mail.cz> wrote in message
I snipped the OPs code. Jim's is easier to read and edit, which I've done.
Did I say that Jim's code was easier to read because of the nice indenting?
I don't know what you are trying to show. Formatting your code and
replacing QString with std::string and changing it so it compiles, it
compiles.
#include <iostream>
#include <map>
#include <string>
You might want to consider a typedef for this
typedef std::map<int,std::string> MyMap; // or a better name
I'd think about putting this whole thing in a function. At the very
least it'll make it easier to test.
void first_last(const MyMap &SeznamPracovniDoby) {
if(SeznamPracovniDoby.empty())
return;
//>> int first=15,latest=15;
How do you know that the first key in the map is 15?
// being at the start is just like being at the
// begining of a new 'typ', so initialize
// typ, first and latest
std::string typ = SeznamPracovniDoby.begin()->second;
int first = SeznamPracovniDoby.begin()->first;
// we're at the start so we initialize latest with first
// we should always do that when we are at the first entry
// of a 'typ'
int latest = first;
for(MyMap::const_iterator i = SeznamPracovniDoby.begin(); i !=
SeznamPracovniDoby.end(); i++) {
These next two lines aren't needed and raise the question of what
happens if the "something" in the first entry in the map has a value of
"". Or what if any entry in the map has a value of ""?
//>> if ( typ == "" )
//>> typ = i->second.c_str();
if (typ!=i->second.c_str())
{
std::cout<<first<<"-"<<latest<<":"<<typ;
did you want a '<< std::endl' at the end of that line?
There are some problems here. If you trace carefully though the code
and maybe add some trace you'll probably see it.
Some problems here too. Same suggestion.
latest=i->first;
typ=i->second.c_str();
}
Maybe this next line
// std::cout << i->first << " " << i->second << std::endl;
Should be
std::cout<<first<<"-"<<latest<<":"<<typ << std::endl;
void test1() {
MyMap m;
// set up some data here and call the function
first_last(m);
}
put test2() and as many others as you want here...
I snipped this and moved it from above
int main()
{
test1();
test2();
// etc..
}
I don't know what you're trying to do though, and I don't know what
QString actually is. Post some compilable code that demonstrates the
problem.
Good idea.
LR