 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue Mar 13, 2007 6:08 pm Post subject: vector |
|
|
Bonjour,
Y'a t il un moyen permettant l'insertion d'un element dans un vector,
tout en gardant l'ordre des elements deja inseres ?
exmple :
vector <int> list_of_integers;
list_of_integers est un ensemble d'entiers triés par ordre
descendant.
sans a avoir a faire un sort a chaue element insere ?
Merci. |
|
| Back to top |
|
 |
Michel Decima Guest
|
Posted: Tue Mar 13, 2007 6:22 pm Post subject: Re: vector |
|
|
ccppc400 (AT) gmail (DOT) com a écrit :
| Quote: | Bonjour,
Y'a t il un moyen permettant l'insertion d'un element dans un vector,
tout en gardant l'ordre des elements deja inseres ?
exmple :
vector <int> list_of_integers;
list_of_integers est un ensemble d'entiers triés par ordre
descendant.
sans a avoir a faire un sort a chaue element insere ?
|
int value = 42;
list_of_integers.insert( lower_bound( list_of_integers.begin(),
list_of_integers.end(),
value,
greater<int>() ),
value ); |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Mar 13, 2007 7:55 pm Post subject: Re: vector |
|
|
Merci,
Dans le cas ou j'ai :
vector <c_objects*> list_of_objects;
*c_objects : contient les operateurs < > == <= >
Comment pourrai-je inserer les element afin de garantir que les
objects ponites par c_objects* sont correctement tries ?
Merci.
On 13 mar, 14:22, Michel Decima <michel.dec...@orange-ft.com> wrote:
| Quote: | ccppc...@gmail.com a écrit :
Bonjour,
Y'a t il un moyen permettant l'insertion d'un element dans un vector,
tout en gardant l'ordre des elements deja inseres ?
exmple :
vector <int> list_of_integers;
list_of_integers est un ensemble d'entiers triés par ordre
descendant.
sans a avoir a faire un sort a chaue element insere ?
int value = 42;
list_of_integers.insert( lower_bound( list_of_integers.begin(),
list_of_integers.end(),
value,
greater<int>() ),
value ); |
|
|
| Back to top |
|
 |
Michel Decima Guest
|
Posted: Tue Mar 13, 2007 8:14 pm Post subject: Re: vector |
|
|
ccppc400 (AT) gmail (DOT) com a écrit :
| Quote: | Merci,
Dans le cas ou j'ai :
vector <c_objects*> list_of_objects;
*c_objects : contient les operateurs < > == <= >=
Comment pourrai-je inserer les element afin de garantir que les
objects ponites par c_objects* sont correctement tries ?
|
template <typename T>
struct indirect_greater : public binary_function<T, T, bool>
{
bool operator()(T lhs, T rhs) const
{
return *lhs > *rhs ;
}
};
c_object* value = new c_object( 42 );
list_of_objects.insert( lower_bound( list_of_objects.begin(),
list_of_objects.end(),
value,
indirect_greater<c_object*>() ),
value ); |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Mar 13, 2007 9:46 pm Post subject: Re: vector |
|
|
Merci.
On 13 mar, 16:14, Michel Decima <michel.dec...@orange-ft.com> wrote:
| Quote: | ccppc...@gmail.com a écrit :
Merci,
Dans le cas ou j'ai :
vector <c_objects*> list_of_objects;
*c_objects : contient les operateurs < > == <=
Comment pourrai-je inserer les element afin de garantir que les
objects ponites par c_objects* sont correctement tries ?
template <typename T
struct indirect_greater : public binary_function<T, T, bool
{
bool operator()(T lhs, T rhs) const
{
return *lhs > *rhs ;
}
};
c_object* value = new c_object( 42 );
list_of_objects.insert( lower_bound( list_of_objects.begin(),
list_of_objects.end(),
value,
indirect_greater<c_object*>() ),
value ); |
|
|
| Back to top |
|
 |
Fabien LE LEZ Guest
|
|
| Back to top |
|
 |
JBB Guest
|
Posted: Fri Mar 16, 2007 2:32 pm Post subject: Re: vector |
|
|
ccppc400 (AT) gmail (DOT) com a écrit :
| Quote: | Bonjour,
Y'a t il un moyen permettant l'insertion d'un element dans un vector,
tout en gardant l'ordre des elements deja inseres ?
exmple :
vector <int> list_of_integers;
list_of_integers est un ensemble d'entiers triés par ordre
descendant.
sans a avoir a faire un sort a chaue element insere ?
Merci.
et avec un set<int> ça le ferait pas mieux? |
(à supposer sue tu ne veuille pas mettre 2 fois la même valeur)
tu n'a plus alors qu'à parcourir le set en sens inverse. |
|
| Back to top |
|
 |
gpg Guest
|
Posted: Tue Apr 17, 2007 2:41 am Post subject: Re: vector [bis] |
|
|
Michel Decima a écrit :
| Quote: | ccppc400 (AT) gmail (DOT) com a écrit :
Merci,
Dans le cas ou j'ai :
vector <c_objects*> list_of_objects;
*c_objects : contient les operateurs < > == <= >=
Comment pourrai-je inserer les element afin de garantir que les
objects ponites par c_objects* sont correctement tries ?
template <typename T
struct indirect_greater : public binary_function<T, T, bool
{
bool operator()(T lhs, T rhs) const
{
return *lhs > *rhs ;
}
};
c_object* value = new c_object( 42 );
list_of_objects.insert( lower_bound( list_of_objects.begin(),
list_of_objects.end(),
value,
indirect_greater<c_object*>() ),
value );
|
Bonjour,
je me suis penché sur la question et voila ce que j'ai fais :
//common.h
#ifndef _common_h_
#define _common_h_
#include <algorithm>
#include <functional>
template <typename T>
struct indirect_greater : public binary_function<T, T, bool>
{
bool operator()(T lhs, T rhs) const
{
return *lhs > *rhs ;
}
};
#endif
//Interface.h
#ifndef _interface_h_
#define _interface_h_
#include "common.h"
#include <vector>
#include <functional>
class C_TRACK
{
public:
C_TRACK(const char* p_track_name);
~C_TRACK();
const char* GetName() const;
bool operator<(const C_TRACK& p_track);
bool operator>(const C_TRACK& p_track);
private:
char m_track_name[100];
};
class C_ALBUM
{
public:
C_ALBUM(const char* p_album_name);
~C_ALBUM();
void AddTrack(C_TRACK* p_track);
void PrintData(void);
private:
char m_album_name[100];
std::vector <C_TRACK*> m_track_list;
};
#endif
//Interface.cpp
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include "interface.h"
C_TRACK::C_TRACK(const char* p_track_name)
{
strcpy(m_track_name,p_track_name);
}
C_TRACK::~C_TRACK()
{
}
const char* C_TRACK::GetName(void) const
{
return (m_track_name);
}
bool C_TRACK::operator<(const C_TRACK& p_track)
{
if (strcmp (m_track_name,p_track.GetName())<0)
{
return (true);
}
else
return (false);
}
C_ALBUM::C_ALBUM(const char* p_album_name)
{
strcpy(m_album_name,p_album_name);
}
C_ALBUM::~C_ALBUM()
{
}
void C_ALBUM::AddTrack(C_TRACK* p_track)
{
m_track_list.insert(lower_bound(m_track_list.begin(),
m_track_list.end(),
p_track,
indirect_greater<C_TRACK*>()),
p_track);
}
void C_ALBUM::PrintData(void)
{
if (!m_track_list.empty())
{
for (unsigned short i=0;i<m_track_list.size();i++)
{
printf("Track %s \n",m_track_list[i]->GetName());
}
}
}
common.h:8: error: expected template-name before '<' token
common.h:8: error: expected `{' before '<' token
common.h:8: error: expected unqualified-id before '<' token
common.h:8: error: expected `;' before '<' token
interface.cpp: In member function `void C_ALBUM::AddTrack(C_TRACK*)':
interface.cpp:48: error: `greater' undeclared (first use this function)
interface.cpp:48: error: (Each undeclared identifier is reported only
once for each function it appears in.)
interface.cpp:48: error: expected primary-expression before '*' token
interface.cpp:48: error: expected primary-expression before '>' token
interface.cpp:48: error: expected primary-expression before ')' token
make.exe: *** [interface.o] Error 1
(voir http://rafb.net/p/aSaOzp25.html)
--
Je ne sais pas ce que j'ai réellement oublié, merci de votre aide. |
|
| Back to top |
|
 |
gpg Guest
|
Posted: Tue Apr 17, 2007 3:21 am Post subject: Re: vector [bis] |
|
|
gpg a écrit :
| Quote: | Michel Decima a écrit :
ccppc400 (AT) gmail (DOT) com a écrit :
Merci,
Dans le cas ou j'ai :
vector <c_objects*> list_of_objects;
*c_objects : contient les operateurs < > == <= >=
Comment pourrai-je inserer les element afin de garantir que les
objects ponites par c_objects* sont correctement tries ?
template <typename T
struct indirect_greater : public binary_function<T, T, bool
{
bool operator()(T lhs, T rhs) const
{
return *lhs > *rhs ;
}
};
c_object* value = new c_object( 42 );
list_of_objects.insert( lower_bound( list_of_objects.begin(),
list_of_objects.end(),
value,
indirect_greater<c_object*>() ),
value );
Bonjour,
je me suis penché sur la question et voila ce que j'ai fais :
//common.h
#ifndef _common_h_
#define _common_h_
#include <algorithm
#include <functional
template <typename T
struct indirect_greater : public binary_function<T, T, bool
{
bool operator()(T lhs, T rhs) const
{
return *lhs > *rhs ;
}
};
#endif
//Interface.h
#ifndef _interface_h_
#define _interface_h_
#include "common.h"
#include <vector
#include <functional
class C_TRACK
{
public:
C_TRACK(const char* p_track_name);
~C_TRACK();
const char* GetName() const;
bool operator<(const C_TRACK& p_track);
bool operator>(const C_TRACK& p_track);
private:
char m_track_name[100];
};
class C_ALBUM
{
public:
C_ALBUM(const char* p_album_name);
~C_ALBUM();
void AddTrack(C_TRACK* p_track);
void PrintData(void);
private:
char m_album_name[100];
std::vector <C_TRACK*> m_track_list;
};
#endif
//Interface.cpp
#include <stdlib.h
#include <stdio.h
#include <string.h
#include <algorithm
#include "interface.h"
C_TRACK::C_TRACK(const char* p_track_name)
{
strcpy(m_track_name,p_track_name);
}
C_TRACK::~C_TRACK()
{
}
const char* C_TRACK::GetName(void) const
{
return (m_track_name);
}
bool C_TRACK::operator<(const C_TRACK& p_track)
{
if (strcmp (m_track_name,p_track.GetName())<0)
{
return (true);
}
else
return (false);
}
C_ALBUM::C_ALBUM(const char* p_album_name)
{
strcpy(m_album_name,p_album_name);
}
C_ALBUM::~C_ALBUM()
{
}
void C_ALBUM::AddTrack(C_TRACK* p_track)
{
m_track_list.insert(lower_bound(m_track_list.begin(),
m_track_list.end(),
p_track,
indirect_greater<C_TRACK*>()),
p_track);
}
void C_ALBUM::PrintData(void)
{
if (!m_track_list.empty())
{
for (unsigned short i=0;i<m_track_list.size();i++)
{
printf("Track %s \n",m_track_list[i]->GetName());
}
}
}
common.h:8: error: expected template-name before '<' token
common.h:8: error: expected `{' before '<' token
common.h:8: error: expected unqualified-id before '<' token
common.h:8: error: expected `;' before '<' token
interface.cpp: In member function `void C_ALBUM::AddTrack(C_TRACK*)':
interface.cpp:48: error: `greater' undeclared (first use this function)
interface.cpp:48: error: (Each undeclared identifier is reported only
once for each function it appears in.)
interface.cpp:48: error: expected primary-expression before '*' token
interface.cpp:48: error: expected primary-expression before '>' token
interface.cpp:48: error: expected primary-expression before ')' token
make.exe: *** [interface.o] Error 1
(voir http://rafb.net/p/aSaOzp25.html)
--
Je ne sais pas ce que j'ai réellement oublié, merci de votre aide.
|
Il me semble que le ompilateur se plaint du fait qu'il n'y a aucun
operateur < prenant des arguments C_TRACK des deux cotés de l'operation . |
|
| Back to top |
|
 |
gpg Guest
|
Posted: Tue Apr 17, 2007 6:54 am Post subject: Re: vector [bis] |
|
|
gpg a écrit :
| Quote: | gpg a écrit :
Michel Decima a écrit :
ccppc400 (AT) gmail (DOT) com a écrit :
Merci,
Dans le cas ou j'ai :
vector <c_objects*> list_of_objects;
*c_objects : contient les operateurs < > == <= >=
Comment pourrai-je inserer les element afin de garantir que les
objects ponites par c_objects* sont correctement tries ?
template <typename T
struct indirect_greater : public binary_function<T, T, bool
{
bool operator()(T lhs, T rhs) const
{
return *lhs > *rhs ;
}
};
c_object* value = new c_object( 42 );
list_of_objects.insert( lower_bound( list_of_objects.begin(),
list_of_objects.end(),
value,
indirect_greater<c_object*>() ),
value );
Bonjour,
je me suis penché sur la question et voila ce que j'ai fais :
//common.h
#ifndef _common_h_
#define _common_h_
#include <algorithm
#include <functional
template <typename T
struct indirect_greater : public binary_function<T, T, bool
{
bool operator()(T lhs, T rhs) const
{
return *lhs > *rhs ;
}
};
#endif
//Interface.h
#ifndef _interface_h_
#define _interface_h_
#include "common.h"
#include <vector
#include <functional
class C_TRACK
{
public:
C_TRACK(const char* p_track_name);
~C_TRACK();
const char* GetName() const;
bool operator<(const C_TRACK& p_track);
bool operator>(const C_TRACK& p_track); private:
char m_track_name[100];
};
class C_ALBUM
{
public:
C_ALBUM(const char* p_album_name);
~C_ALBUM();
void AddTrack(C_TRACK* p_track);
void PrintData(void);
private:
char m_album_name[100];
std::vector <C_TRACK*> m_track_list;
};
#endif
//Interface.cpp
#include <stdlib.h
#include <stdio.h
#include <string.h
#include <algorithm
#include "interface.h"
C_TRACK::C_TRACK(const char* p_track_name)
{
strcpy(m_track_name,p_track_name);
}
C_TRACK::~C_TRACK()
{
}
const char* C_TRACK::GetName(void) const
{
return (m_track_name);
}
bool C_TRACK::operator<(const C_TRACK& p_track)
{
if (strcmp (m_track_name,p_track.GetName())<0)
{
return (true);
}
else
return (false);
}
C_ALBUM::C_ALBUM(const char* p_album_name)
{
strcpy(m_album_name,p_album_name);
}
C_ALBUM::~C_ALBUM()
{
}
void C_ALBUM::AddTrack(C_TRACK* p_track)
{
m_track_list.insert(lower_bound(m_track_list.begin(),
m_track_list.end(),
p_track,
indirect_greater<C_TRACK*>()),
p_track);
}
void C_ALBUM::PrintData(void)
{
if (!m_track_list.empty())
{
for (unsigned short i=0;i<m_track_list.size();i++)
{
printf("Track %s \n",m_track_list[i]->GetName());
}
}
}
common.h:8: error: expected template-name before '<' token
common.h:8: error: expected `{' before '<' token
common.h:8: error: expected unqualified-id before '<' token
common.h:8: error: expected `;' before '<' token
interface.cpp: In member function `void C_ALBUM::AddTrack(C_TRACK*)':
interface.cpp:48: error: `greater' undeclared (first use this function)
interface.cpp:48: error: (Each undeclared identifier is reported only
once for each function it appears in.)
interface.cpp:48: error: expected primary-expression before '*' token
interface.cpp:48: error: expected primary-expression before '>' token
interface.cpp:48: error: expected primary-expression before ')' token
make.exe: *** [interface.o] Error 1
(voir http://rafb.net/p/aSaOzp25.html)
--
Je ne sais pas ce que j'ai réellement oublié, merci de votre aide.
Il me semble que le ompilateur se plaint du fait qu'il n'y a aucun
operateur < prenant des arguments C_TRACK des deux cotés de l'operation .
|
résolu.
Bonne journée. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|