Urgent help on vectors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 105
Reputation: guest7 is an unknown quantity at this point 
Solved Threads: 0
guest7 guest7 is offline Offline
Junior Poster

Urgent help on vectors

 
0
  #1
Mar 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Urgent help on vectors

 
0
  #2
Mar 6th, 2008
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] 
  }
// ...
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 105
Reputation: guest7 is an unknown quantity at this point 
Solved Threads: 0
guest7 guest7 is offline Offline
Junior Poster

Re: Urgent help on vectors

 
0
  #3
Mar 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Urgent help on vectors

 
0
  #4
Mar 6th, 2008
> 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 105
Reputation: guest7 is an unknown quantity at this point 
Solved Threads: 0
guest7 guest7 is offline Offline
Junior Poster

Re: Urgent help on vectors

 
0
  #5
Mar 6th, 2008
Hi,

How can i clear the 2Dimenisonal vector ?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Urgent help on vectors

 
0
  #6
Mar 6th, 2008
You can clear it like this:

  1. 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 ?


Originally Posted by guest7 View Post
Hi,

How can i clear the 2Dimenisonal vector ?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 105
Reputation: guest7 is an unknown quantity at this point 
Solved Threads: 0
guest7 guest7 is offline Offline
Junior Poster

Re: Urgent help on vectors

 
0
  #7
Mar 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Urgent help on vectors

 
0
  #8
Mar 6th, 2008
> I want the size of the vector to be 5x0.
  1. for( size_t i = 0 ; i < event.size() ; ++i )
  2. event[i].clear() ;
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 105
Reputation: guest7 is an unknown quantity at this point 
Solved Threads: 0
guest7 guest7 is offline Offline
Junior Poster

Re: Urgent help on vectors

 
0
  #9
Mar 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 62
Reputation: Joatmon is an unknown quantity at this point 
Solved Threads: 7
Joatmon Joatmon is offline Offline
Junior Poster in Training

Re: Urgent help on vectors

 
0
  #10
Mar 6th, 2008
code for unique function please.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC