OVH Cloud OVH Cloud

pb avec map et visual c++

4 réponses
Avatar
Shaga
Bonjour,
voici un petit programme tout con pour voir comment fonctionne map

#include <map>
#include <iostream>

using namespace std;

int main()
{
typedef map< string , int > strint;

strint si;
strint::iterator i;

for(i=si.begin();i<si.end();i++)
{
cout << i->first << " " << i-> second << endl;
}

return 0;
}

J'obtiens 102 erreurs a la compilation et aucune ne reference mon source

c:\atelier\vc98\include\functional(16) : error C2143: syntax error : missing
'{' before '<'
c:\atelier\vc98\include\functional(16) : error C2059: syntax error : '<'
c:\atelier\vc98\include\functional(22) : error C2143: syntax error : missing
'{' before '<'
c:\atelier\vc98\include\functional(22) : error C2059: syntax error : '<'

Donc ai-je ecris une connerie ou cela vien d'un bug du compilateur (visual
c++ 6 intro) ?

Merci d'avance pour vos reponse.

4 réponses

Avatar
Gabriel Dos Reis
"Shaga" writes:

| Bonjour,
| voici un petit programme tout con pour voir comment fonctionne map
|
| #include <map>
| #include <iostream>
|
| using namespace std;
|
| int main()
| {
| typedef map< string , int > strint;
|
| strint si;
| strint::iterator i;
|
| for(i=si.begin();i<si.end();i++)

un map<T>::iterator est juste bidirectionnel mais pas à accès
direct. En particulier la notion de plus petit ou plus grand n'y est
pas défini. La boucle devrait donc s'écrire

for (i = si.begin(); i != si.end(); ++i)
// ...

Ou, si tu veux te la péter,

struct ValuePrinter {
ValuePrinter(std::ostream& os) : out(&os) { }
void operator()(const strint::value_type& v) const
{
*out << v.first << " " << v.second << endl;
}

private:
std::ostream* out;
};

std::for_each(si.begin(), si.end(), ValuePrinter(std::cout));

-- Gaby
Avatar
Shaga
Merci beaucoup

"Gabriel Dos Reis" a écrit dans le message de
news:
"Shaga" writes:

| Bonjour,
| voici un petit programme tout con pour voir comment fonctionne map
|
| #include <map>
| #include <iostream>
|
| using namespace std;
|
| int main()
| {
| typedef map< string , int > strint;
|
| strint si;
| strint::iterator i;
|
| for(i=si.begin();i<si.end();i++)

un map<T>::iterator est juste bidirectionnel mais pas à accès
direct. En particulier la notion de plus petit ou plus grand n'y est
pas défini. La boucle devrait donc s'écrire

for (i = si.begin(); i != si.end(); ++i)
// ...

Ou, si tu veux te la péter,

struct ValuePrinter {
ValuePrinter(std::ostream& os) : out(&os) { }
void operator()(const strint::value_type& v) const
{
*out << v.first << " " << v.second << endl;
}

private:
std::ostream* out;
};

std::for_each(si.begin(), si.end(), ValuePrinter(std::cout));

-- Gaby


Avatar
Luc Hermitte
Bonsoir.

Gabriel Dos Reis wrote in
news::

struct ValuePrinter {
ValuePrinter(std::ostream& os) : out(&os) { }
void operator()(const strint::value_type& v) const
{
*out << v.first << " " << v.second << endl;
}

private:
std::ostream* out;
};


Question idiote. Pourquoi un pointeur et pas une référence (pour le flux)
?

Juste parce que rouge, ou alors il y a raison cachée à cette approche ?

--
Luc Hermitte <hermitte at free.fr>
FAQ de <news:fr.comp.lang.c++> :
<http://www.cmla.ens-cachan.fr/Utilisateurs/dosreis/C++/FAQ/>
Dejanews : <http://groups.google.com/advanced_group_search>

Avatar
Gabriel Dos Reis
Fabien LE LEZ writes:

| On 20 Aug 2003 14:53:17 +0200, Gabriel Dos Reis
| wrote:
|
| >Content-Type: multipart/mixed; boundary="=-=-="
|
| Toujours des problèmes avec ton lecteur de news ?

je suis pourtant certain lui avoir dit de n'en envoyer les messages
qu'en un seul charset.

-- Gaby