473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

question on map< list<T>::iterator, int>

Consider the program x.cpp :

#include <cstdlib>
#include <iostream>
#include <list>
#include <map>

using namespace std;

int main()
{
map<list<int>::iterator, intm;

list<intc;
c.push_back(100);

// m[c.begin()] = 10;

return EXIT_SUCCESS;
}

This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

However if I remove the comment, I get compilation error because
operator '<' is not defined for list<T>::iterator. Why doesn't the
compiler give this error for
'map<list<int>::iterator, intm;' itself. That is, in this first
statement inside main() itself, the compiler can know that the less-
than operator cannot be defined for the key type. So, why does the
compiler wait for an element to be pushed on to the map to flag the
error ?

Kindly clarify

Thanks
V.Subramanian
Jun 27 '08 #1
9 2200
On Sat, 03 May 2008 14:48:01 +0200, su**************@yahoo.com, India
<su**************@yahoo.comwrote:
Consider the program x.cpp :

#include <cstdlib>
#include <iostream>
#include <list>
#include <map>

using namespace std;

int main()
{
map<list<int>::iterator, intm;

list<intc;
c.push_back(100);

// m[c.begin()] = 10;

return EXIT_SUCCESS;
}

This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

However if I remove the comment, I get compilation error because
operator '<' is not defined for list<T>::iterator. Why doesn't the
compiler give this error for
'map<list<int>::iterator, intm;' itself. That is, in this first
statement inside main() itself, the compiler can know that the less-
than operator cannot be defined for the key type. So, why does the
compiler wait for an element to be pushed on to the map to flag the
error ?

Kindly clarify

Thanks
V.Subramanian
A map is alway sorted by <.
To use an objet like a key for a std::map, these objet must have defined
operator<.
Jun 27 '08 #2
su**************@yahoo.com, India wrote:
[example code snipped]
However if I remove the comment, I get compilation error because
operator '<' is not defined for list<T>::iterator. Why doesn't the
compiler give this error for
'map<list<int>::iterator, intm;' itself. That is, in this first
statement inside main() itself, the compiler can know that the less-
than operator cannot be defined for the key type. So, why does the
compiler wait for an element to be pushed on to the map to flag the
error ?
This was discussed not too long ago in a thread named
"Inserting objects into a std::map?":

http://groups.google.com/group/comp....0d5cd88?lnk=st

When you create a map without ever inserting, why *should* the compiler
complain?
Jun 27 '08 #3
On 2008-05-03 08:48:01 -0400, "su**************@yahoo.com, India"
<su**************@yahoo.comsaid:
>
This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

However if I remove the comment, I get compilation error because
operator '<' is not defined for list<T>::iterator. Why doesn't the
compiler give this error for
'map<list<int>::iterator, intm;' itself. That is, in this first
statement inside main() itself, the compiler can know that the less-
than operator cannot be defined for the key type. So, why does the
compiler wait for an element to be pushed on to the map to flag the
error ?
The compiler doesn't compile template code that isn't used, so it
doesn't notice the missing operator< until it's used.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #4
>
This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
Why We use These Flags.?

Jun 27 '08 #5
an**************@gmail.com wrote:
>This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
Why We use These Flags.?
To get a reasonable level of warnings.

--
Ian Collins.
Jun 27 '08 #6
On 2008-05-04 07:43, an**************@gmail.com wrote:
>>
This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
Why We use These Flags.?

-std=c++98 means that we want to compile the code using the C++ 98
standard, -pedantic enables warnings for everything that should produce
a warning and rejects usages of private extansions. -Wall and -Wextra
turns on lots of warnings in case you do something dubious. If your code
compiles without producing any warnings you can be quite sure you have
not made anything obviously bad, though there are a lot of things that
the complier can not check for you.

--
Erik Wikström
Jun 27 '08 #7
On May 4, 2:07 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-05-04 07:43, andrew.smith....@gmail.com wrote:
This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
Why We use These Flags.?

-std=c++98 means that we want to compile the code using the C++ 98
standard, -pedantic enables warnings for everything that should produce
a warning and rejects usages of private extansions. -Wall and -Wextra
turns on lots of warnings in case you do something dubious. If your code
compiles without producing any warnings you can be quite sure you have
not made anything obviously bad, though there are a lot of things that
the complier can not check for you.

--
Erik Wikström
Thanks A Lot Erik :)
Jun 27 '08 #8
On May 3, 2:48 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider the program x.cpp :
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
using namespace std;
int main()
{
map<list<int>::iterator, intm;
list<intc;
c.push_back(100);
// m[c.begin()] = 10;
return EXIT_SUCCESS;
}
This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
However if I remove the comment, I get compilation error because
operator '<' is not defined for list<T>::iterator. Why doesn't the
compiler give this error for
'map<list<int>::iterator, intm;' itself.
G++ does, if you give it the proper options. Try adding
-D_GLIBCXX_CONCEPT_CHECKS, for example. (This is with g++ 4.1.
I've heard that the option is not necessary with some later
versions, or at least with some builds of some later versions.)
Not providing the ordering relationship is undefined behavior,
howver, and a compiler is not required to diagnose it. Not all
do.
That is, in this first statement inside main() itself, the
compiler can know that the less- than operator cannot be
defined for the key type. So, why does the compiler wait for
an element to be pushed on to the map to flag the error ?
Because it can? The standard doesn't require a diagnostic.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #9
On May 4, 11:07 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-05-04 07:43, andrew.smith....@gmail.com wrote:
This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
Why We use These Flags.?
-std=c++98 means that we want to compile the code using the
C++ 98 standard, -pedantic enables warnings for everything
that should produce a warning and rejects usages of private
extansions.
Not entirely. It does turn off some very portable "future"
features, like long long, but it leaves some g++ extensions
active, including cases which render the compiler non-conformant
active.
-Wall and -Wextra turns on lots of warnings in case you do
something dubious.
And some which aren't dubious at all:-).

More to the point: if you're just learning C++, you should
probably invoke it however your instructor says to invoke it,
without worrying too much about why. Otherwise, you should
definitely read the documentation, and understand what each
option does, and why you want it, or not. For a variety of
reasons, all compilers require a fairly long list of options in
practice---200 or 300 hundred characters seems to be about a
minimum. (I need long long in my work, for example, so I can't
use -pedantic. But I can turn on most of what -pedantic
encompasses individually.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
7
by: Andrew Robinson | last post by:
I have two List<t>. I need to search ListA to see if it contains ListB So: ListA { 1, 2, 3, 4, 5, 6, 7, 8, 9 } Searching ListA with { 4, 5, 6 } would return true and an index of 3....
13
by: Steve Edwards | last post by:
Hi, Given a map: typedef map<long, string, greater<long> > mapOfFreq; Is there a quicker way to find the rank (i.e. index) of the elememt that has the long value of x? At the moment I'm...
2
by: per9000 | last post by:
Hi, *background* I want a class containing an int (a list of sets of integer). This should be hidden for the user and he/she should be able to insert his/her favourite data structure so to be a...
7
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me...
2
by: darkkal | last post by:
Hi I'm a student majoring a computer . I tried to make a simple hashing table like (int) 1 , (std::string) "One" There is a problem in using std::map ㅜㅜ this is a simple code. PLZ~~Help...
0
by: subramanian100in | last post by:
consider the following program: #include <cstdlib> #include <iostream> #include <map> #include <utility> #include <string> using namespace std;
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
3
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.