Vectors

HI, I'm working with vectors, but I don't understand it at all its all so confusing, I simply don't know how to use it =[. So what I'm working with is a txt file and it has data, I was able to get the data into a vector.

(This is my main.cpp)
string line
ifstream file;
file.open(argv[1]);
while (file >> line) {
list.push_back(line);
}
for (int i=0; i<list.size(); ++i) {
cout << list.at(i) << " ";
}
file.close();
cout <<endl;

With the above I was able to get the data and output it in command line.
But I also want to use those vectors in other .h files for void functions.
so my question is how should I write it so that I can access these vectors?
I have been at this for a few hours now, and I'm completely stumped =[
Vector or any other container type if you want to use it efficiently can't be explained in one topic,

Your best bet is studding this reference for example:
http://www.cplusplus.com/reference/stl/vector/
It explains almost all the major methods comonly used for standard operations.
You pass vectors like any other object:
void foo(const std::vector<std::string>& vec)...
Thank You, Athar for helping with that.
Codekiddy list is helpful but I'm still running into a lot of issues.

I have the void function within a class, the other problem that I'm running into is that I can't declare a vector within a class, when I compile it, it yields a long list of errors that i cannot understand.

Last edited on
A header should look like this for example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include <vector>

class A
{
public:
       A()
       {
            mV.push_back(55);  
       }

       void f(std::vector<short>& mV2)
       {
              mV2.push_back(300);
       }
private:
       std::vector<int> mV;
};


If you post your error output maybe someone can explain your problem better.
Last edited on
Topic archived. No new replies allowed.