0

I am trying to iterate through my vector array, but I cannot seem to get it to work, my error is at the second for loop and the error message is the follow:

expected primary expression before 'double'

I have looked through on how to iterate a regular vector, but how do I iterator a vector array? I followed this structure:

#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;
  for (int i=1; i<=5; i++) myvector.push_back(i);

  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

I can't seem to get my vector array version to work.

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main(int arc, char *argv[])
{
   vector<double> vector[7];

   double num[7];
   ifstream infile("data.txt"); 
   string temp;

   for(int i = 0; i <= 6; i++)
   {
      infile >> temp;
      cout << temp << ' ';
   }
   cout << endl;

   while(infile >> num[0] >> num[1] >> num[2] >> num[3] >> num[4] >> num[5] >> num[6])
   {
      for(int i = 0; i <= 6; i++)
      {
         vector[i].push_back(num[i]);
      }
   }
   cout << endl;

   for(int i = 0; i <= 6; i++)
   {
      // error on this line
      // not sure what is wrong before vector<double>:: iterator it = vector[i].begin()
      for(vector<double>::iterator it = vector[i].begin(); it != vector[i].end(); ++it)
      {
         cout << ' ' << *it;
      }
   }
   return 0;
}
5
  • 1
    vector<double> my_vector[7]; Try to avoid using namespace std to prevent this from happening. Commented Jun 21, 2013 at 4:06
  • 5
    Calling a vector variable "vector" is not a good idea. Give it a name that isn't taken, and the problem will go away. Commented Jun 21, 2013 at 4:12
  • Also for each vector you push back only one number, is that what you really want? i.e. 7 vectors for 7 doubles. Commented Jun 21, 2013 at 4:15
  • @Beta, after changing the name "vector" to "v" it did the trick. Thanks. I did not even consider that, it's like doing int int = 5. Commented Jun 21, 2013 at 4:26
  • c++11 for(double d:v)//stuff; ftw Commented Jun 21, 2013 at 4:33

3 Answers 3

2

On this line of code

for(vector<double>::iterator it = vector[i].begin(); it != vector[i].end(); ++it)

vector is not a type, it's a variable. Names in local scopes hide names in global scopes.

You could instead write

for(::std::vector<double>::iterator it = vector[i].begin(); it != vector[i].end(); ++it)

or

for(auto it = vector[i].begin(); it != vector[i].end(); ++it)

but a better solution is to not use the same name for multiple different things.

Sign up to request clarification or add additional context in comments.

Comments

1
#include <iostream>
#include <fstream>
#include <vector>

using std::ifstream;
using std::vector;
using std::cout;

int main(int arc, char *argv[])
{
   const size_t data_size = 7;
   ifstream infile("data.txt"); 
   vector<double> input;
   double num;
   while ((infile >> num) && (input.size() <= data_size))
   {
      input.push_back(num);
   }
   for (vector<double>::iterator it = input.begin(); it != input.end(); ++it)
   {
      cout << ' ' << *it;
   }
   return 0;
}

Use minimal variables and get the job one. Also don't name your variables after standard library functions, classes, structs, etc. and avoid using directives like using namespace std; and prefer using declarations.

2 Comments

"don't name your variables after standard library functions, classes, structs" - Why? Given that the standard library uses a whole lot of names (and many of them being very general ones), this advice is extremely counter-productive. What do think namespaces are for?
@ChristianRau: I understand that namespaces effectively hide these names, but readability is hampered when uses exactly the same name. This, perhaps, is the reason we see custom written vectors to have names like EAVector, etc. with a prefix to differentiate from standard containers. When this is the case for even classes, variables of the same name as its class is even more confusing when 1000s of loc.
-1

You can simply traverse a vector of array like this for (int i=0; i<myvector.size(); i++) { for (int j=0; j<myvector.size(); j++) { cout<<myvector[i][j]<<endl; } }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.