2D vector function

I am trying to pass a vector to a function and the function print out the vector to the console window.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

void ProcessInput(const char *, vector<vector<int>> *);

int main()
{
  char inCharArry[100];
  int iRow = 5;
  int iCol = 5;
  vector<vector<int>> inVec(iRow, vector<int>(iCol));

  for (int i = 0; i < inVec.size(); ++i){
	  for (int j  = 0; j < inVec[i].size(); ++j){
		  inVec[i][j] = 1;
	  }
  }


  cout << "Calling   function ProcessInput()" << endl;
  ProcessInput(inCharArry, &inVec);
  cout << "Back from function ProcessInput()" << endl;

  return 0;
}	

void ProcessInput(const char inChar[], vector<vector<int>> *invector)
{
using namespace std;

	for(int i = 0; i < invector->size(); ++i){
		for(int j = 0; j < invector[i]->size(); ++j){
			
		}
		
	}
  cout<< "In function ProcessInput()" << endl;
}

I got an error : error c2819: type 'std::vector<_Ty>'does not have an overloaded member 'operator->'

Any help would be appreciated. Thank you so much in advance.
1
2
3
4
5
6
7
8
9
10
11
12
void ProcessInput(const char inChar[], vector<vector<int>> *invector)
{
using namespace std;

	for(int i = 0; i < invector->size(); ++i){
		for(int j = 0; j < invector[i].size(); ++j){
			
		}
		
	}
  cout<< "In function ProcessInput()" << endl;
}


This looks really odd - anyway - vector<vector<int>> *invector <int> you didn't declare as a pointer so no need for the arrow '->' use the dot '.'
careful:

for(int j = 0; j < invector[i].size(); ++j){

That is not doing what you think. invector is a pointer (to a vector<vector>) so 'i' is not indexing your vector as you are trying to do... instead, it's indexing the POINTER (ie: accessing invalid/nonexistant objects)

This would actually need to be for(int j = 0; j < (*invector)[i].size(); ++j){


OR you could make your life easier / less error prone and pass by reference instead of by pointer:

1
2
3
4
5
6
7
8
9
10
void ProcessInput(const char inChar[], vector<vector<int> >& invector)
{
  for(int i = 0; i < invector.size(); ++i)
  {
    for(int j = 0; j < invector[i].size(); ++j)
    {
      // much better
    }
  }
}
Thank you so much for quick response.

i forgot to add one more line.

Here is what i wanna do.

1. make a 2d vector with ones and zeros in main().
2. pass this vector to a function.
3. print this vector to console window.

This is the full code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

void ProcessInput(const char *, vector<vector<int>> *);

int main()
{
  char inCharArry[100];
  int iRow = 5;
  int iCol = 5;
  vector<vector<int>> inVec(iRow, vector<int>(iCol));

  for (int i = 0; i < inVec.size(); ++i){
	  for (int j  = 0; j < inVec[i].size(); ++j){
		  inVec[i][j] = iRow;
	  }
  }


  cout << "Calling   function ProcessInput()" << endl;
  ProcessInput(inCharArry, &inVec);
  cout << "Back from function ProcessInput()" << endl;

  return 0;
}	

void ProcessInput(const char inChar[], vector<vector<int>> *invector)
{
using namespace std;

	for(int i = 0; i < invector->size(); ++i){
		for(int j = 0; j < invector[i].size(); ++j){
			cout << invector[i][j] << " ";
		}
		cout << endl;
	}
  cout<< "In function ProcessInput()" << endl;
}


I got an error at line 38; error c2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>'

see my post.

for(int j = 0; j < invector[i].size(); ++j){

This does not do as you expect


cout << invector[i][j] << " ";

neither does this. invector[i][j] is not an integer from the vector, it's a vector

invector is type vector< vector<int> >*

invector[i] is type vector< vector<int> >

invector[i][j] is type vector<int>

Pass this vector by reference instead of by pointer.
Last edited on
You are awesome. The following code works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

void ProcessInput(const char *, vector<vector<int>> invector);

int main()
{
  char inCharArry[100];
  int iRow = 5;
  int iCol = 5;
  vector<vector<int>> inVec(iRow, vector<int>(iCol));

  for (int i = 0; i < inVec.size(); ++i){
	  for (int j  = 0; j < inVec[i].size(); ++j){
		  inVec[i][j] = iRow;
	  }
  }

  cout << "Calling   function ProcessInput()" << endl;
  ProcessInput(inCharArry, inVec);
  cout << "Back from function ProcessInput()" << endl;

  return 0;
}	

void ProcessInput(const char inChar[], vector<vector<int>> invector)
{
using namespace std;

vector<vector<int>> ivTemp = invector;

	for(int i = 0; i < invector.size(); ++i){
		for(int j = 0; j < invector[i].size(); ++j){
			cout << invector[i][j] << " ";
		}
		cout << endl;
	}
  cout<< "In function ProcessInput()" << endl;
}


But i have one more question.

As I mentioned above, I need to do
1. read text data, which is actually adjacency list representing a graph.
2. store the graph representations with zeros and ones (zero means there is no edge, one means there is an edge) to a certain data structure.

In that case, is 2D vector a good approach? I could do it with Matlab and it wasnt that difficult but with c++ it is a different story. Any suggestion would be appreciated!
eek! now you're passing by value!

no no, pass by reference (or even in this case const reference because you're not changing invector)

void ProcessInput(const char inChar[], const vector<vector<int> >& invector)

notice the & symbol.

Also why are you making a copy of the vector (ivTemp)?


Anyway, as for whether or not it's a good approach. That depends on who you ask. Personally I'm not a fan of using 2D anything for gridlike structures. I wrote a biased article on the subject here: http://cplusplus.com/forum/articles/17108/
Last edited on
Topic archived. No new replies allowed.