Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

vector

9 réponses
Avatar
ccppc400
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=E9s par ordre
descendant.
sans a avoir a faire un sort a chaue element insere ?

Merci.

9 réponses

Avatar
Michel Decima
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 );

Avatar
ccppc400
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 wrote:

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



Avatar
Michel Decima
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 );

Avatar
ccppc400
Merci.

On 13 mar, 16:14, Michel Decima wrote:

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



Avatar
Fabien LE LEZ
http://www.usenet-fr.net/fr.usenet.reponses/usenet/repondre-sur-usenet.html
Avatar
JBB
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.

Avatar
gpg
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.


Avatar
gpg
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 .



Avatar
gpg
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.