By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,045 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

vector of (int,string). can't convert i->first to int

P: n/a
int first=15,latest=15; QString typ=NULL;
std::map<int,std::string>::iterator i;
for(i = SeznamPracovniDoby.begin(); i != SeznamPracovniDoby.end(); i++)
{ if (typ==NULL) typ=i->second.c_str();
if (typ!=i->second.c_str()) {
std::cout<<first<<"-"<<latest<<":"<<typ; first=i->first; }
else { latest=i->first; typ=i->second.c_str(); }
std::cout << i->first << " " << i->second << std::endl;
}
Aug 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Milan Krejci" <rd*@no-spam.mail.czwrote in message
news:f9***********@news.vol.cz...
int first=15,latest=15; QString typ=NULL;
std::map<int,std::string>::iterator i;
for(i = SeznamPracovniDoby.begin(); i != SeznamPracovniDoby.end();
i++)
{ if (typ==NULL) typ=i->second.c_str();
if (typ!=i->second.c_str()) { std::cout<<first<<"-"<<latest<<":"<<typ;
first=i->first; }
else { latest=i->first; typ=i->second.c_str(); }
std::cout << i->first << " " << i->second << std::endl;
}
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>

int main()
{
std::map<int,std::stringSeznamPracovniDoby;

int first=15,latest=15;
std::string typ;
std::map<int,std::string>::iterator i;
for(i = SeznamPracovniDoby.begin(); i != SeznamPracovniDoby.end(); i++)
{
if ( typ == "" )
typ = i->second.c_str();
if (typ!=i->second.c_str())
{
std::cout<<first<<"-"<<latest<<":"<<typ;
first=i->first;
}
else
{
latest=i->first;
typ=i->second.c_str();
}
std::cout << i->first << " " << i->second << std::endl;
}

}

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.
Aug 15 '07 #2

P: n/a
hello thanks for the reply.
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==20, latest==31, typ=="something else".
QString was a QT toolkit's implementation of a string class.

Jim Langston napsal(a):
"Milan Krejci" <rd*@no-spam.mail.czwrote in message
news:f9***********@news.vol.cz...
>int first=15,latest=15; QString typ=NULL;
std::map<int,std::string>::iterator i;
for(i = SeznamPracovniDoby.begin(); i != SeznamPracovniDoby.end();
i++)
{ if (typ==NULL) typ=i->second.c_str();
if (typ!=i->second.c_str()) { std::cout<<first<<"-"<<latest<<":"<<typ;
first=i->first; }
else { latest=i->first; typ=i->second.c_str(); }
std::cout << i->first << " " << i->second << std::endl;
}

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>

int main()
{
std::map<int,std::stringSeznamPracovniDoby;

int first=15,latest=15;
std::string typ;
std::map<int,std::string>::iterator i;
for(i = SeznamPracovniDoby.begin(); i != SeznamPracovniDoby.end(); i++)
{
if ( typ == "" )
typ = i->second.c_str();
if (typ!=i->second.c_str())
{
std::cout<<first<<"-"<<latest<<":"<<typ;
first=i->first;
}
else
{
latest=i->first;
typ=i->second.c_str();
}
std::cout << i->first << " " << i->second << std::endl;
}

}

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.

Aug 15 '07 #3

P: n/a
First you don't have a vector of anything.
It's a map.

Some points:

Initialize i inside the for unless you have
good reason to do otherwise.

Hopefully QString can handle being initialized
and compared to NULL. std::string won't.

Hopefully QString can be assigned a const char* and
there is a ostring << QString operator defined.

The if(typ==NULL) typ = i->second.c_str()
line looks problematic. It causes you not
to do the setting of "first" the first time
through the loop. It looks to me you want to
delete this case and set typ to second.c_str()
inside the body of the next if. This assumes
that second will never contain something that
compares to a NULL typ.
Aug 15 '07 #4

P: n/a
LR
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==20, 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" <rd*@no-spam.mail.czwrote 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::stringMyMap; // 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?
> first=i->first;
There are some problems here. If you trace carefully though the code
and maybe add some trace you'll probably see it.

> }
else
{
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
Aug 15 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.