Dynamic Arrays / Vectors
Aug 2, 2013 at 9:42pm Aug 2, 2013 at 9:42pm UTC
Hi everyone,
I have a C++ question that is seemingly simple but I cannot find an answer to my situation. How do you create an array if you don't know its size ahead of time? Specifically, I want to read in files of different sizes, and want the array to only be as big as the number of elements in those files. From what I've seen with vectors and dynamic arrays, you still have to specify a size for the array. Let's say I'm reading in files and have no idea how many numbers they contain, just that they contain some list of numbers. And I want to read in each value and store it into an array.
Here's just a snippet of code. The first thing I need to do is figure out how to store the numbers from the list that's read in so that I can do calculations with those numbers later.
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
double numbers[x];
ifstream inFile("data1.txt" );
if (inFile.fail())
{
cout<<"File failed to open." ;
exit(1);
}
while (!inFile.eof())
{
inFile>>numbers[x];
}
I've found bits and pieces of other code but I don't know how to apply it to this situation, or if it's what I need at all:
std::vector<double >numbers;
1 2 3
int *darray = new int [size];
delete [] darray
int *numbers= new int [size];
Can someone please explain / show me how I would store multiple numbers when I don't know how many there will be ahead of time? Thank you so much!
Aug 2, 2013 at 9:44pm Aug 2, 2013 at 9:44pm UTC
Vectors are meant for this purpose.
1 2 3 4 5 6 7 8
std::vector<double > myVec;
double numbers = 0;
while (!inFile.eof())
{
inFile >> numbers;
myVec.push_back(numbers);
}
Aug 2, 2013 at 9:52pm Aug 2, 2013 at 9:52pm UTC
Thank you so much. But what if I actually need two sets of numbers per line, i.e. a multidimensional array:
1 2 3 4 5 6 7 8 9 10 11 12
std::vector<double > v;
double coordinates;
ifstream inFile("data1.txt" );
while (!inFile.eof())
{
int x, y;
inFile>>coordinates[x][y];
v.push_back(coordinates);
}
Then how would I write it?
Aug 2, 2013 at 10:20pm Aug 2, 2013 at 10:20pm UTC
std::vector<std::vector<double >> myVec;
For me it's more helpful to think of this as a vector of vectors rather than multidimensional. You can populate it like this:
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 45
#include <iostream>
#include <vector>
int main ()
{
std::vector<std::vector<double >> mainVec;
std::vector<double > elementVec;
double input = 0;
size_t elementInputLimit = 0;
std::cout << "Enter -1 to exit\n" ;
while (input != -1)
{
std::cout << "Enter a number to populate vector: " ;
std::cin >> input;
elementVec.push_back(input);
++elementInputLimit;
if (elementInputLimit == 5)
{
mainVec.push_back(elementVec);
elementVec.clear();
elementInputLimit = 0;
}
}
int vectorNum = 0;
for (auto outerSrch : mainVec)
{
std::cout << "Vector element number " << vectorNum << "\n" ;
for (auto innerSrch : outerSrch)
{
std::cout << innerSrch << "\n" ;
}
++vectorNum;
}
return 0;
}
Topic archived. No new replies allowed.