Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1388 connectés 

  FORUM HardWare.fr
  Programmation

  [C++] Comment associer un pointeur à vector<bool>?

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C++] Comment associer un pointeur à vector<bool>?

n°42374
Alload
Posté le 24-06-2001 à 10:22:57  profilanswer
 

J'ai essayé ça:
 
vector<bool>* array;
array = new vector<bool>(50);
 
Mais ça ne marche pas.
 
Comment faire ça?

mood
Publicité
Posté le 24-06-2001 à 10:22:57  profilanswer
 

n°42390
janoscoder
Posté le 24-06-2001 à 14:30:15  profilanswer
 

comment ça ça ne marche pas. Je pense que ça marche, mais que ça ne fait pas ce que tu veux.
Si tu veux faire un tableau de bool, tu peux prendre la classe que je t'ai donnée, qui n'est autre qu'un tableau bidimensionnel de bool. Si tes lignes de bool ont des tailles différentes, pense à:
 
vector<vector<bool> > tabboolé(50, vector<bool>(70));
et là t'as un vector de 50 vector de 70 bool.
ah oui, car on peut construire un vecteur avec
vector<truc>  monvec(70); //un vecteur de 70 trucs
ou
vector<truc> monvec(70, untruc); //et le vector est rempli de 70 copies de untruc.
 
Pense que les vectors remplacent avantageusement les array [] du C (sauf qqes cas demande moi), et qu'ils évitent tout new et delete.
 
Regarde le code que je t'ai mailé, il n'est pas trop balèze.


---------------
-----------------------
n°42392
Alload
Posté le 24-06-2001 à 14:42:30  profilanswer
 

J'ai pas encore étudié le code que tu m'as passé, je vais faire ça quand j'aurais une grande plage de temps :D
 
J'ai corrigé une erreur que j'avais, mais maintenant se pose le problème de changer la valeur d'un vector<bool> et de la sortir.
 
Enfin bref, vaut mieux regarder le code, il est pas long:
 
#include <vector>
#include <iostream>
using namespace std;
 
 
class CBool
{
 int width;
 int height;
 vector<bool>* array;
 
public:
 CBool(int x, int y) {width = x; height = y; array = new vector<bool>(width*height);}
 void Set(int x, int y, vector<bool> value) {array[x+y*width] = value;}
 vector<bool> Get(int x, int y) {return array[x+y*width];}
};
 
 
int main()
{
 CBool tab(1024, 768);
 tab.Set(50, 50, true);
 
 if (tab.Get(50, 50) == true)
  cout << "true" << endl;
 
 else if (tab.Get(50, 50) == false)
  cout << "false" << endl;
 
 else  
  cout << "marche pas" << endl;
 
 while (1)
 {
 }
 
 return 0;
}
 
 
En fait, il veut pas faire marcher les "=" et les "==":
 
error C2664: 'Set' : cannot convert parameter 3 from 'const bool' to 'class std::vector<bool,class std::allocator<bool> >'
 
error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'const bool' (or there is no acceptable conversion)
 
error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'const bool' (or there is no acceptable conversion)

n°42393
janoscoder
Posté le 24-06-2001 à 14:47:28  profilanswer
 

justement, c'est là que t'aurais dû regarder le code que je t'ai filé.
Un vector, ce n'est pas un pointeur sur quelque chose, c'est un conteneur qui contient des bools rangés séquentiellement.
genre
 
vector<bool> tab(3);
tab[0]=true;
tab[1]=false;
tab[2]=true;
if (tab[0]||(tab[1]&&tab[2])
   cout << "vrai";
 
par exemple. Y'a pas d'allocation et de désallocation à faire, le vecteur fait tout ça tout seul.
Je tape de post suivant avec du code qui marchera pour toi et qui sera court.


---------------
-----------------------
n°42394
janoscoder
Posté le 24-06-2001 à 14:52:19  profilanswer
 

class Tabbit2D: public vector<bool>
{
 int h,l;
public:
 Tabbit2D(int th, int tl): h(th), l(tl), vector<bool>(th*tl){}
 Tabbit2D(int th, int tl, bool vinit): h(th), l(tl), vector<bool>(th*tl, vinit){}
 bool & operator () (int x, int y)
  {
    return operator[] (h*y+x);
  }
 const bool & operator () (int x, int y) const
  {
    return operator[] (h*y+x);
  }
};
 
ton code devient donc :
 
int main()  
{  
Tabbit2D tab(1024, 768);  
tab(50, 50)=true;  
 
if (tab(50, 50) == true)  
  cout << "true" << endl;  
 
else if (tab(50, 50) == false)  
  cout << "false" << endl;  
 
else  
  cout << "marche pas" << endl;  
 
while (1)  
{  
}  
 
return 0;  
}

 

[edit]--Message édité par janoscoder--[/edit]


---------------
-----------------------
n°42409
Alload
Posté le 24-06-2001 à 18:29:34  profilanswer
 

Voilà, j'ai codé ma fonction qui crée un bitmask et la class du tableau hébergeant ce bitmask grâce à janoscoder, mais le problème c'est que même si je mets un pixel true lors de la création du bitmask, si j'affiche la valeur du pixel dans la fonction Main elle sera false.
 
Tous les bits sont false, alors qu'ils devraient tous être true, je comprend vraiment pas. Surtout que j'ai fais des tests lors de l'éxécution de la fonction et les pixels sont bien détectés.
 
#include <windows.h>
#include <vector>
#include <iostream>
using namespace std;
 
#define for if(1) for
 
///////////////////////////////////////////////////////////////////
 
class CBoolArray: public vector<bool>
{
 short width;
 short height;
 
public:
 CBoolArray(short x, short y): width(x), height(y), vector<bool>(x*y){}
 CBoolArray(short x, short y, bool value): width(x), height(y), vector<bool>(x*y, value){}
 bool & operator () (short x, short y) {return operator[] (x+y*width);}
 const bool & operator () (short x, short y) const {return operator[] (x+y*width);}
 short GetWidth() {return width;}
 short GetHeight() {return height;}
};
 
///////////////////////////////////////////////////////////////////
 
CBoolArray tab(1024, 768);
 
///////////////////////////////////////////////////////////////////
 
int CreateBitmask(CBoolArray bMask, char szBitmap[], short rtrans, short gtrans, short btrans)
{
 HDC hdcImage;
 HBITMAP hbm;
 short width = bMask.GetWidth();
 short height = bMask.GetHeight();
 
 int pixelvalue = 0;
 
 hbm = (HBITMAP) LoadImage(GetModuleHandle(NULL), szBitmap, IMAGE_BITMAP, width,
                              height, LR_CREATEDIBSECTION);
    if (hbm == NULL)
        hbm = (HBITMAP) LoadImage(NULL, szBitmap, IMAGE_BITMAP, width, height,
                                  LR_LOADFROMFILE | LR_CREATEDIBSECTION);
 
 if (hbm == NULL)
  return 1;
 
    hdcImage = CreateCompatibleDC(NULL);
    SelectObject(hdcImage, hbm);
 
 for (short y = 0; y < height; y++)
 {
  for (short x = 0; x < width; x++)
  {
   short r = 0;
   short g = 0;
   short b = 0;
 
   pixelvalue = GetPixel(hdcImage, x, y);
 
   r = (pixelvalue & 0xFF);
   g = (pixelvalue & 0xFF00)>>8;
   b = (pixelvalue & 0xFF0000)>>16;
 
   if (r != rtrans || g != gtrans || b != btrans)
    bMask(x, y) = true;
 
   else  
    bMask(x, y) = false;
  }
 }
 
 return 0;
}
 
///////////////////////////////////////////////////////////////////
 
int main()
{
 CreateBitmask(tab, "bitmap.bmp", 255, 0, 255);
 
 if (tab(50, 50) == true)
  cout << "true" << endl;
 
 if (tab(50, 50) == false)
  cout << "false" << endl;
 
 while (1)
 {
 }
 
 return 0;
}

n°42410
verdoux
And I'm still waiting
Posté le 24-06-2001 à 18:52:37  profilanswer
 

Hum, tu connais la différence entre passage par valeur et par référence en c++ ?

n°42412
Alload
Posté le 24-06-2001 à 19:12:46  profilanswer
 

Non :)

n°42413
verdoux
And I'm still waiting
Posté le 24-06-2001 à 19:22:47  profilanswer
 

Ben il faut écrire:
int CreateBitmask(CBoolArray& bMask, char szBitmap[], short rtrans, short gtrans, short btrans)  
:)

n°42414
Alload
Posté le 24-06-2001 à 19:26:44  profilanswer
 

Ah oki, donc si on rajoute le & toutes les opérations vont êtres transmises à la class spécifiée, et si on ne met pas le & les opérations vont être transmises à une nouvelle class qui sera détruite à la fin de la fonction. C'est ça?

mood
Publicité
Posté le 24-06-2001 à 19:26:44  profilanswer
 

n°42432
janoscoder
Posté le 24-06-2001 à 22:13:42  profilanswer
 

c'est ça
et définis
short GetWidth() {return width;}  
short GetHeight() {return height;}  
plutôt comme
short GetWidth() const {return width;}
et idem pour GetHeight.
 
de plus, les short, ça va jusqu'à 65536, et comme la taille des données est grande devant la taille des shorts, autant utiliser des unsigned int, ça évite les dépassements, mais je pinaille.
 
Je te suggère d'implémenter une fonction de redimensionnement après construction, car c'est bien pratique.


---------------
-----------------------

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation

  [C++] Comment associer un pointeur à vector<bool>?

 

Sujets relatifs
petit pointeur deviendra grand !!!!!!!!![VC++] Comment associer la touche "entrée" à un bouton d'une box ?
[VB] associer un checkbox avec un champ d'une table accessremplir un vector avec un iterator
[ c++ ] pointeur sur fonctionBOOL et bool ?
[VB] pointeur souris et interpolationstring et vector
VB : Intercepter les evenements souris (pointeur)ssnniifff....j'ai perdu mon pointeur !!!
Plus de sujets relatifs à : [C++] Comment associer un pointeur à vector<bool>?


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR