Solved

how to put array into vector?

Posted on 2004-10-15
14
213 Views
Last Modified: 2013-12-14
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
Comment
Question by:tom_mk
14 Comments
 
LVL 11

Accepted Solution

by:
avizit earned 100 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

by:SteH
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

by:tom_mk
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

by:SteH
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

by:SteH
SteH earned 100 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 30

Expert Comment

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

Expert Comment

by:Axter
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

by:Axter
Axter earned 100 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

by:OnegaZhang
OnegaZhang earned 100 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

by:kalmorah
kalmorah earned 100 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

by:Axter
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now