• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

how to put array into vector?

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
tom_mk
Asked:
tom_mk
5 Solutions
 
avizitCommented:
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
 
SteHCommented:
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
 
tom_mkAuthor Commented:
i wanna put array of element into vect
so the diagram shoulb b like this


myVectoor ->>>  {arrayA , arrayB, arrayC}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
SteHCommented:
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
 
SteHCommented:
The changed line is
vector<double*> vec;
For your case the vector should store pointers to double values and not double values.
0
 
AxterCommented:
Sounds like you want an array of an array.
A two dimensional vector array.
0
 
AxterCommented:
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
 
AxterCommented:
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
 
OnegaZhangCommented:
//  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
 
kalmorahCommented:
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
 
AxterCommented:
>>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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now