-4

all

I am using vector in C++ STL to store my data. I pass and return them into and from functions. However, as the data size grows, the program is slower and slower. Thus I am updating the codes to an "iterator version".

What I want to archieve is that use iterators to pass, return and iterate STL vectors.

I am now ok with the operations with 1-dimensional vector, just like manipulating the arrays. However, when it comes to 2-dimensional vector, I am a bit confused.

Can anyone show me a simple code example that how to iterate a 2D vector using STL iterator?

Many thanks in advance.

Regards

Long

ChangeMyName
  • 7,018
  • 14
  • 56
  • 93
  • Don't forget you can pass things by const ref to avoid copies. Can you post a code sample of a 2D vector. Is it a vector>? – doctorlove Aug 02 '13 at 08:53
  • -1, your question asks for sample code; sample code for the problem is already available on stackoverflow – dans3itz Aug 02 '13 at 08:54
  • @doctorlove Thank you very much for your replies. Now I figure out the question and get it running properly. However, another question raised: How to get the size of a vector using its iterator? – ChangeMyName Aug 02 '13 at 09:03
  • Impossible. But why do you need that anyway? – doctorlove Aug 02 '13 at 09:06
  • @doctorlove I use it in order to improve the speed performance of my code. The data chunk I use is usually very big. Thus passing the data variables is very very very time consuming. Thus I though passing the iterator can help me. You've mentioned const reference. May I know what is that idea? – ChangeMyName Aug 02 '13 at 09:35
  • @user2633803 I've added an answer - I think you need to read a basic book about C++. Try "Accelerated C++" – doctorlove Aug 02 '13 at 09:42

3 Answers3

1

Well its already somewhere on stackoverflow

But if you don't want to search here it is :

std::vector<std::vector<int> >  vec{ {1,2,3},{4,5,6}};

//Simplest Way:- (C++11)

for(auto row:vec)
{
  for(auto col:row)
   std::cout<<col<< " ";
  std::cout<<std::endl;
}

//OR Using iterator
std::vector<std::vector<int> >::iterator r;
std::vector<int>::iterator c;
for (r = vec.begin(); r != vec.end(); r++) {
    for (c = r->begin(); c != r->end(); c++) {
        std::cout<<*c<< " ";
    }
    std::cout<<std::endl;
}

Can get distance only between two iterators of same container

std::vector<int>::iterator s = v2.begin(); //Can be any start
std::vector<int>::iterator e = v2.end(); // Can be any end

std::cout<<"Distance :"<<std::distance(s,e)<<std::endl;
P0W
  • 46,614
  • 9
  • 72
  • 119
1

You state that your basic problem is performance, right? You assume that this is caused due to copying. Perhaps there could be simpler solutions for your problem:

  • Check if vectors can be passed by (const) reference
  • Check if shared_ptr makes sense
  • Consider if move semantics can help
  • Perhaps compiler version or implementation prevent return value optimization
SebastianK
  • 3,582
  • 3
  • 30
  • 48
1

If you need to know the size of a vector, and have two iterators it1 it2,

std::distance(it1, it2);

will tell you the distance between them. This will happen to be the size if they are begin and end

If you have a function like

int work(std::vector<int> items)
{
  //...
}

this copies the vector items, so will use more RAM and take longer. Sending a const ref instead will not copy the vector. Making it const stops you changing it, which might not help you, but you haven't posted any code so I don't know what you want to do.

int work(const std::vector<int> & items)
{
  //...
}
doctorlove
  • 18,872
  • 2
  • 46
  • 62