473,508 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4 2162
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
6142
by: Sergio del Amo | last post by:
Hi, I am trying to create a web site to work in Opera 7.2, Explorer 6.0 and Mozilla 1.4. I have the code: var imgs= document.getElementsByTagName("img"); alert(imgs.id); The id of imgs is...
14
2716
by: Alex Vinokur | last post by:
Here is some function that detects if a vector contains only different elements bool vector_contains_only_different_elements (const vector<int>& v) { for (int i = 0; i < v.size(); i++) { if...
34
4128
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
13
1512
by: Ivar | last post by:
Hi guys - So basically I am trying to implement a function that converts an int to a string, but it is not working for some reason - any thoughts? My function, intToStr, is shown below. I'm just...
10
14066
by: ypjofficial | last post by:
Hello All, In my program I am using a pointer to a vector vector<XYZ> * vptr = new vector<XYZ>; and also the XYZ class has a char* as one of its member.I have created all the copy...
3
5840
by: eriwik | last post by:
I use the following structure to store filenames for one or more "sets" grouped together by a number: map<int, map<string> > fileSets; As arguments to the constructor I send a...
4
3060
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
1
2073
by: Alan | last post by:
I am wondering if anyone has any better idea of how to approach this problem than I do. . . . I have a vector of items (data). I have to do a pairwise comparison of each item to each other item...
1
72966
by: rdraider | last post by:
I can't seem to find a way to convert an INT type in the form of YYYYMMDD to an actual date form of mm/dd/yyyy Can anyone pointt me in the right direction? Thanks
2
9642
karthickkuchanur
by: karthickkuchanur | last post by:
if((document.forms.employeeId.value == 0) && (document.forms.applicantId.value == 0)) { alert("Please Select the "+document.forms.type.value+" Code or Name"); return false; } i...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.