Making a two(2) dimensional vector

The code works great, but I am trying to add the length of the each name to the vector. Can someone please show me how to declare a two(2) dimensional vector? I simply want to modify the existing vector to have another dimension of number?

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
string fileName,line;
ifstream infile;
cout<<"Enter the file name and complete path: ";
cin>>fileName;
int Aa=0,count=0;
std::vector<string> A;
infile.open(fileName.c_str());
while(!infile.eof())
{
getline(infile,line);
if(line[count]=='A'||line[count]=='a')
{
A.push_back(line);
A[Aa]=line;
Aa++;
const size_t size = A.size();
for(size_t i=0; i<size; ++i)
{
for (size_t j=0; j<size-i-1;j++)
{
if (A[j+1].length() < A[j].length())// ascending order simply changes to <
{
A[j].swap(A[j+1]);
}
}
}
}
}
return 0;
}
You mean something like this?

1
2
3

std::vector< std::vector<double> > vect2d(600, std::vector<double>(800,0)) ;
Can you please explain each section for me, it looks very complicated. Also what is the significance of the numbers (600, 800, 0).
It's a 2d vector of double. Similar to:

 
double arr[600][800]; 


Hope this helps!

Edit: 0 is what it is initialized to.
Last edited on
What is the "0". And also I did not want to declare a size due to the fact that I don't know what size to expect. Can this be done without declaring a size?
0 is what it is initialized to.

Can this be done without declaring a size?

Yes.

 
std::vector< std::vector<double> > vec2d; 
My current one dimensional vector is of type STRING. However, in my two dimensional vector, I would like to have one time as STRING and another as either INT or DOUBLE. Can I have a two dimensional vector with two different types? How do I declare it?
um. Not that I know off. However, what you can do is something like below:

1
2
3
4
5
6
7
8
9

struct types { 

    int           _i ; 
    std::string   _s ; 
} ; 

std::vector<types> vec; 
Last edited on
The golder rule of arrays: in order to allow random access, all elements must be of the same type.
Topic archived. No new replies allowed.