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

M

Milan Krejci

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;
}
 
J

Jim Langston

Milan Krejci said:
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::string> SeznamPracovniDoby;

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.
 
M

Milan Krejci

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):
 
R

Ron Natalie

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.
 
L

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):

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?


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


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.

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..
}



Good idea.

LR
 
F

Frank Birbacher

Hi!

Milan said:
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".

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top