Hello everyone,
i've gone blank atm and i cant think

i have a class

class Client 
{
      public:
             int aa;
             int bb;
      
};

different file

class agent
{
      private:
              vector<Client> clientRequest;
      public:
             void getClient();
};

and i get information from a file

infile >> C.aa;
        infile.get(c);
        infile >> C.bb;
        infile.get(c);

how would i concatenate aa and bb into the vector?
clientRequest.push_back(????);

Recommended Answers

All 8 Replies

>clientRequest.push_back(????);
Well, I guess you could just create a temporary instance of Client that you continually fill with data and push onto the vector.

It'd be much easier to add a new element into the vector with insert , and then just fill in the data directly:

clientRequest[i].aa = xx;
clientRequest[i].bb = yy;

Better yet, instead of using the [] as vector accessors, use std::iterator.

mmm maybe not using aa and bb would of been better, say clientID and clientName and then add to the vector by doing

clientRequest.push_back(aClient);

iam not sure how to put clientID and clientName into aClient

>
Better yet, instead of using the [] as vector accessors, use std::iterator.

Probably to learn iterators. But otherwise vector being the C++ array one should use the subscript operator for accessing rather than iterator. It's faster.

this would probably be more readable.
and as efficient with a decent compiler.

struct client 
{
      explicit inline client( int a, int b, const std::string& n ) 
                                 : aa(a), bb(b), name(n) {}
      int aa;
      int bb;
      std::string name ;
};

extern std::vector<client> vec ;
extern std::istream file ;
int a, b ; std::string name ;
file >> a >> b >> name ;
vec.push_back( client(a,b,name) ) ;

well this is my code so far

class studentDetail
{
        int StudID;
        int StudMark;
}
class Student
{
      private:
              vector<studentDetail> studentRequest;
      public:
             Student(){void clear();}
             void getStudent();
};
                  
void Student::getClient()
{
ifstream infile;
  infile.open("test.txt");

  if (infile.fail())
    cout << "File doesn't exist ";
  else {
       Student S;
       char c;
     while(!infile.eof()) {
        infile >> S.StudID;
        infile.get(c);
        infile >> S.StudMark;
        infile.get(c);

        //clientRequest.push_back();
        
      infile.get(c);   
     } //end while
     infile.close();
  } //end if
}

i cant find a way to put studid and studmark in to push_back so when u search for studid studmark is linked to it

// .....
  else {
       Student S;
       char c;
     while(!infile.eof()) {
        infile >> S.StudID;
        infile.get(c);
        infile >> S.StudMark;
        infile.get(c);

        clientRequest.push_back( S );
        
      infile.get(c);   
     } //end while
// .....
clientRequest.push_back( S );

ahh that was stupid of me..thanks

another question, is it possible to store and array in a vector?
eg an a array of tests

another question, is it possible to store and array in a vector?
eg an a array of tests

not directly (C arrays do not have value semantics).
however, you could simulate value semantics by wrapping the array in a structure. eg.

template< typename T, size_t N > struct array
{
  T& operator[] ( size_t pos ) { return a[pos] ; }
  const T& operator[] ( size_t pos ) const { return a[pos] ; }
  T a[N] ;
};
std::vector< array<double,50> > container ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.