std:.vector<> problem!

Started by
6 comments, last by Impz0r 22 years, 5 months ago
y00, I will swap two cells agains each other, but i failed can someone tell me how does it work?! Btw i use the std vector Thanks alot... Mfg Impz0r
Stay Evil & Ugly!
Advertisement
Need more specifics, like if you''re swapping two entire vectors, just two indices, what your code looks like etc. Anyway, according to the SGI STL swap documentation,
#include <algorithm>#include <vector>using namespace std;vector<type_name> v;vector<type_name> w;...swap(v[x], v[y]);...swap(w,v); 

should both work, since STL includes specialized versions of swap for all containers.


I wanna work for Microsoft!
ok i will try to explain...

this is my struct
// Serverlist Colum
typedef struct tagServerlistColum
{
eServerlistColumTypes eType;
Char *cName;
uInt32 uiIndex;
Boolean bList;
} TServerlistColum, *PServerlistColum;

then my arrays

vector m_aAvailableColums;
vector m_aCurrentColums;

first i will swap a cell between the both arrays, and inside one array.

erm... this is how i try it, but it wont work..

TServerlistColum *pFirstColum = m_aCurrentColums.at (iIndex-1);
TServerlistColum *pSecondColum = m_aCurrentColums.at (iIndex);
if (!pFirstColum || !pSecondColum)
return;

// FFS HACK ka
vector ::iterator SwapColum;
vector ::iterator i = m_aCurrentColums.begin ();
vector ::iterator End = m_aCurrentColums.end ();
while (i != End)
{
if ((*i) == pFirstColum)
{
++(*i)->uiIndex;
SwapColum = i;
}
if ((*i) == pSecondColum)
{
// decrease index
--(*i)->uiIndex;
// swap colums
swap (SwapColum, i);
break;
}
++i;
}

please help me ...thanks

Mfg Impz0r
Stay Evil & Ugly!
You do realize that the objects you wish to swap need to be assignable? For example, can you do this in your code?
TServerlistColum *q;// initialize q// obtain x...m_aAvailableColums[x] = q; 

If you can''t, swap() will fail.

Just out of curiosity, why are you using the old C-style struct syntax? In C++, a struct is a class whose access specifier defaults to public (as opposed to private when you use the ''class'' keyword). In other words, you could implement an assignment operator in your TServerlistColum class, which might solve the swap problem...


I wanna work for Microsoft!
I have a similar problem. I am using a vector to store a bunch of objects of a homemade class type. When I try to use
  sort(myvector.begin(), myvector.end());  

I get 15 errors telling me:
! Error E2093 c:\borland\bcc55\include\algorith.h 636: ''operator<'' not implmemented in type ''cMyClass'' for arguments of the same type in function __median 

I found a tutorial about STL telling me I need to overload some operators, but when I tried that it didn''t seem to work (I get the same errors again). I must not be overloading the operator properly. Could someone help me here?
I want to sort the objects based on one member of the class (below it is the private member "position"):
  class cMyClass  {  private:    int  position;    // other stuff, not important  public:    int operator< (cMyClass tMC) {return (tMC.position < position);};    // other stuff, not important  };  

I would infinitely appreciate any help here, of course
The correct declaration for the overload is:
  bool operator < (const Type &rhs) const;  


Or, there is a predicate version of all sorting functions and containers that will allow you to define your own binary predicate:
  struct Pred{  bool operator () (const Type &lhs, const Type &rhs);};  
y00,

ok, i got it

Thank you Oluseyi for your help in ths case

Mfg Impz0r
Stay Evil & Ugly!
Stoffel,
I used your correct declaration above, and it works (of course, you knew that didn''t you?)! Thank you!

This topic is closed to new replies.

Advertisement