| | |
Urgent help on vectors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 105
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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] } // ...
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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
if the keys do change, use an associative container like
> 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 •
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
You can clear it like this:
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 ?
C++ Syntax (Toggle Plain Text)
event.clear();
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> I want the size of the vector to be 5x0.
C++ Syntax (Toggle Plain Text)
for( size_t i = 0 ; i < event.size() ; ++i ) event[i].clear() ;
•
•
Join Date: Mar 2008
Posts: 105
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: SNMP Libraries
- Next Thread: SCORING SYSTEM C++
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






