[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# how to put array into vector?

Posted on 2004-10-15
Medium Priority
252 Views
says i have this code (below)
and the lline that has beed read has multiline (says 10 lines)
so array wil b inserted the val 10 tims.
and each time when array has been successfully inserted the data

i want that-time array to insert to the vect

so my vector, each position will contain array.
and i can call each item directly.

btw, can any show me how to do this?

Thx
TOm

-=================================
void main()
{
//int a,b,c,d,e;
double t[20];
vector <double> vec;
ifstream infile("c://a.txt");

if(infile)
{
while(!infile.eof())
{
infile >> t[0] >> t[1] >> t[2] >> t[3] >> t[4] >> t[5] >> t[6] >> t[7] >> t[8] >> t[9] >> t[10] >> t[11] >> t[12] >> t[13] >> t[14] >> t[15] >> t[16] >> t[17] >> t[18] >> t[19] >> t[20];

vec.push_back(t);
}

for(int i = 0; i < 20 ; i++)
{
cout << t[i] << "\n";

}

// a = Vector.valueOf(i);
}

}
0
Question by:tom_mk

LVL 11

Accepted Solution

avizit earned 400 total points
ID: 12317158
once you have the array filled up you can use a loop to put the values in the vector

for(unsigned i = 0; i < size_of_array; i++){

vec.push_back(t[i]);
}

0

LVL 13

Expert Comment

ID: 12317252
Why not remove the array and use

double dTmp;
while (infile) {
infile >> dTmp;
vec.push_back (dTmp);
cout << dTmp << endl;
}

to get the vector filled?

0

Author Comment

ID: 12317352
i wanna put array of element into vect
so the diagram shoulb b like this

myVectoor ->>>  {arrayA , arrayB, arrayC}
0

LVL 13

Expert Comment

ID: 12317357
Then you need to use:

int main()              // main should return int according to C/C++ standards.
{
//int a,b,c,d,e;
double t[20];
vector <double*> vec;
ifstream infile("c://a.txt");

if(infile)
{
while(!infile.eof())
{
infile >> t[0] >> t[1] >> t[2] >> t[3] >> t[4] >> t[5] >> t[6] >> t[7] >> t[8] >> t[9] >> t[10] >> t[11] >> t[12] >> t[13] >> t[14] >> t[15] >> t[16] >> t[17] >> t[18] >> t[19] >> t[20];

vec.push_back(t);
}

for(int i = 0; i < 20 ; i++)
{
cout << t[i] << "\n";

}

// a = Vector.valueOf(i);
}
return 0;
}
0

LVL 13

Assisted Solution

SteH earned 400 total points
ID: 12317364
The changed line is
vector<double*> vec;
For your case the vector should store pointers to double values and not double values.
0

LVL 30

Expert Comment

ID: 12317490
Sounds like you want an array of an array.
A two dimensional vector array.
0

LVL 30

Expert Comment

ID: 12317585
Example code:

void main()
{
double t[20];
vector<vector<double> > vec;
ifstream infile("c://a.txt");

if(infile)
{
while(!infile.eof())
{
infile >> t[0] >> t[1] >> t[2] >> t[3] >> t[4] >> t[5] >> t[6] >> t[7] >> t[8] >> t[9] >> t[10] >> t[11] >> t[12] >> t[13] >> t[14] >> t[15] >> t[16] >> t[17] >> t[18] >> t[19] >> t[20];

vec.push_back(vector<double>(t, t+20));
}

for(vector<vector<double> >::iterator i = vec.begin();i != vec.end();++i)
{
for(vector<double>::iterator d = i->begin();d != i->end();++d)
{
cout << (*d);
}
cout << "\n";
}
}

}
0

LVL 30

Assisted Solution

Axter earned 400 total points
ID: 12317621
Notice in the above code vec is the following type:
vector<vector<double> > vec;

This is a vector of vector type double.

On the push back command you use the following command to push the entire t variable array:
vec.push_back(vector<double>(t, t+20));

FYI:
A vector of type vector<double*> will not work for your current code, because you'll end up having a vector in which all the iterators point to t[20] variable.  That would mean that your vector would have the same value for every push_back item, and the value would be the last set of values pulled from the file.
0

LVL 12

Assisted Solution

OnegaZhang earned 400 total points
ID: 12332126
//  The following can be built with Dev-C++ and VC2003
#include <fstream>
#include <string>
#include <deque>
#include <iomanip>
#include <iostream>
#include <vector>
#include <sstream>

template<typename T>
int parse_line(std::string line, T& que)
{
//return number of float parsed from the line
int existing  = que.size();
std::string separators = " ";
int n = line.length();
int start, stop;
start = line.find_first_not_of(separators);
int field_index = 0;
while ((start >= 0) && (start < n))
{
stop = line.find_first_of(separators, start);
if ((stop < 0) || (stop > n))
stop = n;
std::string field = line.substr(start, stop - start);
std::stringstream oss;
oss<<field;
float val = 0;
oss>>val;
que[field_index] = (val);
field_index++;
start = line.find_first_not_of(separators, stop+1);
}
int nret = field_index;
return nret;
}
int main()
{
std::ifstream input("C:\\Dev-Cpp\\Examples\\array\\data.txt");
std::deque<std::string> lines;
std::string line;
int fields_per_row = 0;
bool bfound = false;
typedef std::vector<double> doublevector;
std::vector<doublevector > vec_array(10);
for(int i=0;i<10;i++)
vec_array[i] = doublevector(20); // initialize the vector with vector
while(getline(input,line) && bfound==false)
{
doublevector& valarray=vec_array[lines.size()];
lines.push_back(line);
fields_per_row = parse_line(line,valarray);
}
std::cout<<"line number:"<<lines.size()<<std::endl;
for(int i=0;i<lines.size();i++)
{
doublevector& valarray = vec_array[i];
for(int j=0;j<20;j++)
std::cout<<std::setw(4)<<std::setfill(' ')<<valarray[j];
std::cout<<std::endl;
}
std::cout<<"access the vector of vector as if it is a two-dimension array  "<<vec_array[0][17]<<std::endl;
std::cout<<std::endl;
std::cout<<std::endl;
system("PAUSE");
}

/*
g++ -c parse.cpp
g++ -o parse parse.o

welcome to www.fruitfruit.com
*/
0

Assisted Solution

kalmorah earned 400 total points
ID: 12333809
You should be reading in from the file to your array all at once.

After this you can move those values to a vector 1 by 1.

Here's an example. (You must supply your own a.txt)

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

using namespace std;

void main()
{
int i = 0; //counter
double t[20];
vector <double> vec;
cout << "opening file...";
ifstream infile("a.txt");
if (infile)
cout << "DONE\n";
else {
cout << "File will not open.\n";
exit(1);
}

while(!infile.eof()) {
infile >> t[i];
i++;
}
for(i = 0; i < 20; i++) vec.push_back(t[i]);

for(int i = 0; i < 20 ; i++) cout << t[i] << "\n";
}
0

LVL 30

Expert Comment

ID: 12357919
>>You should be reading in from the file to your array all at once.

Not if it's an array of an array, which seems to be the case from the posted code.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallā¦
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a ā¦
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month19 days, 5 hours left to enroll

#### 834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.