[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how to put array into vector?

Posted on 2004-10-15
14
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 11

Accepted Solution

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

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 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

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 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

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

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

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

650 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