Hi,

I have a two dimensional vector and I am trying to access all the elements of one row at a time. I am getting a syntax error. Following is my code :


std::vector<int> row;
std::vector<std::vector<int> > event(5,row);

for(lv = 0; lv < 5; lv++)
{
for(int gno=event[lv].begin; gno != event[lv].end(); gno++)
{
//doing something
}

I am getting the following error while compiling:

error: cannot convert '__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >' to 'int' in initialization
error: no match for 'operator!=' in 'gno != (+ event_gate. std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >](((long unsigned int)(lv / 5))))->std::vector<_Tp, _Alloc>::end [with _Tp = int, _Alloc = std::allocator<int>]()'

Please help! I am really stuck

Thanks

Recommended Answers

All 11 Replies

std::vector<int> row;
std::vector<std::vector<int> > event(5,row);
// note: every row is empty at this point

for(lv = 0; lv < event.size() ; ++lv )
{
  for( int gno=0 ; gno < event[lv].size(); ++gno )
  {
     // doing something
     // event[lv][gno] 
  }
// ...

Hi,

Thanks for quick reply. I have a small question. For better performance of the program (in terms of memory utilization and time for running the program) it is better to use vectors or data structure? Which data structure is best for fast search operation too?

Thanks

> For better performance of the program (in terms of memory utilization
> and time for running the program) it is better to use vectors ...
a vector is just about the most efficient resizeable sequence container.

> Which data structure is best for fast search operation too?
if the keys on which you want to search are not volatile (ie. you fill the vector once and then they do not change), just use a std::binary_search after sorting the vector on keys (use std::sort for this)
if the keys do change, use an associative container like std::map

Hi,

How can i clear the 2Dimenisonal vector ?

Thanks

You can clear it like this:

event.clear();

Notice if you do this, Not only the data stored is erased, also All elements is cleared, this might not be what you want to do ?

Hi,

How can i clear the 2Dimenisonal vector ?

Thanks

Hi,

If the size of my 2D vector is 5x5 (say) i want the vector to be empty and have no element in it . In other words I want the size of the vector to be 5x0.

What should i use?

Thanks

> I want the size of the vector to be 5x0.

for( size_t i = 0 ; i < event.size() ; ++i )
    event[i].clear() ;

Hi,

Thanks for the help. I have one more problem in the program. I have some non-unique values in the rows of 2 dimensional vectors. I am using unique to get the unique elements from the row. But i am not getting the unique elements. Following is my code:

std::vector<int> row;
std::vector<std::vector<int> > event(5,row);
std::vector<int>::iterator p, p_end;

for(lv=0; lv<5; lv++)
{
        p_end = unique(event[lv].begin(), event[lv].end());

        for(p = event[lv].begin(); p < p_end; p++) 
       {
        doing something
       }
}

I think p_end should point to the last unique element. But it is not working. What is wrong in the code?

Thanks

code for unique function please.

> I am using unique to get the unique elements from the row.
> But i am not getting the unique elements
the std::unique algorithm checks each two consecutive elements for equivalence; if you have two equivalent elements that are not next to each other, the second one will not be removed. std::unique does not work as you expect on a non-sorted range.

It seems you have two options.
1. Sort the array then use std::unique.
or
2. Make your own function to copy unique values into an array and use a loop to check the values while going through them. (actually it would probably be good to use a vector as it is the best resizable data container so I've heard.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.