Make vector large enough to hold all value : vector size « Vector « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Vector » vector sizeScreenshots 
Make vector large enough to hold all value
  
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

class PC
{
   public:
   enum part keyboard, mouse, monitor };

   PCpart a_part = PC::keyboard, int id = );
   bool operator<const PC& rhs const;

   void print() const;

   private:
   part part_;
   int id_;
};

inline
PC::PCpart a_part, int id : part_a_part ), id_id ){}

inline bool PC::operator<const PC& rhs const{  
   return id_ < rhs.id_; 
}

void PC::print() const {
   string component;
   ifpart_ == keyboard )
      component = "keyboard";
   else ifpart_ == mouse )
      component = "mouse";
   else
      component = "monitor";

   cout << "ID: " << setw<< left << id_ << " PC: " << component << endl;
}

int main( )
{
   list<PC> listA;
   listA.push_backPCPC::keyboard, ) );
   listA.push_backPCPC::mouse, ) );
   listA.push_backPCPC::monitor, ) );
   listA.push_backPCPC::keyboard, ) );
   listA.push_backPCPC::monitor, ) );

   list<PC> inspector_BlistA );
   inspector_B.front() = PCPC::mouse, );
   inspector_B.back() = PCPC::monitor, );

   // must sort before using set algorithms
   listA.sort();
   inspector_B.sort();

   vector<PC> result;

   result.resizelistA.size() + inspector_B.size() );
}
  
    
  
Related examples in the same category
1.Computing the sum with template iterators
2.vector size before and after elements insertion
3.vector: max_size(), size(), capacity()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.