标签:class priority 模板库 扩容 oid 访问 下标 hat ring
#include <vector> #include <algorithm> #include "print.h" class A { public: A (int i = 0) : m_i (i) { cout << "无参构造:" << this << endl; } A (const A& that) : m_i (that.m_i) { cout << "拷贝构造:" << &that << "->" << this << endl; } A& operator= (const A& that) { cout << "拷贝赋值:" << &that << "->" << this << endl; if (&that != this) m_i = that.m_i; return *this; } ~A (void) { cout << "析构函数:" << this << endl; } operator int& (void) { return m_i; } /* operator const int& (void) const { return static_cast<int&> ( const_cast<A&> (*this)); } */ bool operator== (const A& that) const { return m_i == that.m_i; } /* bool operator< (const A& that) const { return m_i < that.m_i; } */ bool operator() (const A& a, const A& b) const { return a.m_i < b.m_i; } private: int m_i; }; int main (void) { cout << "---- 1 ----" << endl; vector<A> va (3); cout << "---- 2 ----" << endl; va.push_back (A ()); cout << "---- 3 ----" << endl; va.erase (va.begin ()); cout << "---- 0 ----" << endl; va[0] = A (10); va[1] = A (50); va[2] = A (70); va.push_back (A (60)); vector<A>::iterator it = find (va.begin (), va.end (), A (70)); if (it == va.end ()) cout << "没找到!" << endl; else cout << "找到了:" << *it << endl; // sort (va.begin (), va.end ()); sort (va.begin (), va.end (), va[0]); print (va.begin (), va.end ()); return 0; }
#include <deque> #include <algorithm> #include "print.h" int main (void) { deque<int> di; di.push_back (24); di.push_back (33); di.push_front (18); di.push_front (68); di.insert (di.begin () + 2, 47); print (di.begin (), di.end ()); // 68 18 47 24 33 di.pop_back (); di.pop_front (); di.erase (di.begin () + 1); print (di.begin (), di.end ()); // 18 24 di.push_back (20); sort (di.begin (), di.end ()); print (di.begin (), di.end ()); // 18 20 24 size_t size = di.size (); for (size_t i = 0; i < size; ++i) cout << di[i] << ‘ ‘; cout << endl; di.resize (10); print (di.begin (), di.end ()); return 0; }
fe.cpp
#include <iostream> //#include <algorithm> using namespace std; template<typename IT, typename DOIT> void for_each (IT begin, IT end, DOIT doit) { while (begin != end) doit (*begin++); } void print (int& x) { cout << x << endl; } void add (int& x) { ++x; } int main (void) { int a[5] = {1, 2, 3, 4, 5}; for_each (a, a + 5, print); for_each (a, a + 5, add); for_each (a, a + 5, print); return 0; }
#include <algorithm> #include <vector> #include "print.h" template<typename iterator, typename type> iterator find (iterator begin, iterator end, const type& key) { for (; begin != end; ++begin) if (*begin == key) break; return begin; } bool cmpInt (const int& a, const int& b) { return a > b; } class CmpInt { public: CmpInt (bool less = true) : m_less (less) {} bool operator() (const int& a, const int& b) const{ return m_less ? (a < b) : (a > b); } private: bool m_less; }; int main (void) { int ai[] = {10, 20, 30, 40, 50}; vector<int> vi (ai, &ai[5]); print (vi.begin (), vi.end ()); vector<int>::iterator it = vi.begin (); it = vi.insert (it + 1, 15); print (vi.begin (), vi.end ()); ++++++it; it = vi.erase (it); print (vi.begin (), vi.end ()); cout << *it << endl; // 50 vi.insert (vi.begin (), 37); vi.insert (vi.begin () + 2, 43); vi.insert (vi.begin () + 4, 29); vi.push_back (18); vi.push_back (24); print (vi.begin (), vi.end ()); sort (vi.begin (), vi.end ()); print (vi.begin (), vi.end ()); sort (vi.begin (), vi.end (), /*cmpInt*/CmpInt (false)); print (vi.begin (), vi.end ()); it = ::find (vi.begin (), vi.end (), 18); if (it == vi.end ()) cout << "没找到!" << endl; else cout << "找到了:" << *it << endl; return 0; }
list.cpp
#include <list> #include "print.h" int main (void) { list<int> li; li.push_back (34); li.push_back (28); li.push_back (34); li.push_back (34); li.push_back (55); li.push_back (34); print (li.begin (), li.end ()); li.unique (); print (li.begin (), li.end ()); li.sort (); print (li.begin (), li.end ()); li.unique (); print (li.begin (), li.end ()); list<int> li2; li2.push_front (100); li2.push_front (200); li2.push_front (300); list<int>::iterator pos = li.begin (); ++pos; // li.splice (pos, li2); list<int>::iterator start = li2.begin (); ++start; // li.splice (pos, li2, start); list<int>::iterator end = li2.end (); li.splice (pos, li2, start, end); print (li.begin (), li.end ()); cout << li2.size () << endl; return 0; }
#ifndef _PRINT_H #define _PRINT_H #include <iostream> using namespace std; template<typename iterator> void print (iterator begin, iterator end) { while (begin != end) cout << *begin++ << ‘ ‘; cout << endl; } #endif // _PRINT_H
#include <iostream> #include <vector> using namespace std; void print (const vector<int>& vi) { cout << "大小:" << vi.size () << endl; cout << "容量:" << vi.capacity () << endl; for (vector<int>::const_iterator it = vi.begin (); it != vi.end (); ++it) cout << *it << ‘ ‘; cout << endl; } int main (void) { vector<int> vi (5, 3); print (vi); vi.push_back (4); print (vi); vi[6] = 100; cout << vi[6] << endl; vi.push_back (5); cout << vi[6] << endl; vi.resize (12); print (vi); vi.resize (2); print (vi); vi.clear (); print (vi); vi.reserve (20); print (vi); cout << vi[19] << endl; vi.reserve (5); print (vi); return 0; }
#include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; class Movie { public: friend istream& operator>> (istream& is, Movie& movie) { return is >> movie.m_title >> movie.m_comp >> movie.m_gross; } friend ostream& operator<< (ostream& os, const Movie& movie) { return os << movie.m_title << ‘ ‘ << movie.m_comp << ‘ ‘ << movie.m_gross; } bool operator< (const Movie& movie) const { return gross () > movie.gross (); } private: double gross (void) const { string str (m_gross); size_t pos = 0; while ((pos = str.find_first_of ("$,", pos)) != string::npos) str.erase (pos, 1); return atof (str.c_str ()); } string m_title; string m_comp; string m_gross; }; bool read (const char* file, vector<Movie>& vm) { ifstream ifs (file); if (! ifs) { perror ("打开票房文件失败"); return false; } Movie movie; while (ifs >> movie) vm.push_back (movie); ifs.close (); return true; } bool write (const char* file, const vector<Movie>& vm){ ofstream ofs (file); if (! ofs) { perror ("打开排行文件失败"); return false; } for (vector<Movie>::const_iterator it = vm.begin(); it != vm.end (); ++it) ofs << *it << endl; ofs.close (); return true; } int main (int argc, char* argv[]) { if (argc < 3) { cerr << "用法:" << argv[0] << " <票房文件> <排行文件>" << endl; return -1; } vector<Movie> vm; if (! read (argv[1], vm)) return -1; sort (vm.begin (), vm.end ()); if (vm.size () > 10) vm.resize (10); if (! write (argv[2], vm)) return -1; return 0; }
#include <iostream> #include <vector> using namespace std; class A { public: A (int i = 0) : m_i (i) {}; int m_i; }; void print (const vector<int>& vi) { size_t size = vi.size (); cout << size << endl; for (size_t i = 0; i < size; ++i) cout << vi[i] << ‘ ‘; cout << endl; } int main (void) { vector<int> vi; vi.push_back (10); vi.push_back (20); vi.push_back (30); vi.push_back (20); vi.push_back (10); print (vi); vi.pop_back (); print (vi); ++vi.front (); vi.back () = 100; cout << vi.front () << ‘ ‘ << vi.back () << endl; typedef vector<int>::iterator IT; typedef vector<int>::const_iterator CIT; typedef vector<int>::reverse_iterator RIT; typedef vector<int>::const_reverse_iterator CRIT; for (IT it = vi.begin (); it != vi.end (); ++it) ++*it; const vector<int>& cvi = vi; for (CIT it = cvi.begin (); it != cvi.end (); ++it) cout << /*--*/*it << ‘ ‘; cout << endl; for (CRIT it = cvi.rbegin (); it!=cvi.rend(); ++it) cout << *it << ‘ ‘; cout << endl; vector<string> vs; vs.push_back ("beijing"); vs.push_back ("tianjin"); vs.push_back ("shanghai"); cout << *(vs.begin () + 2) << endl; *const_cast<char*> ((vs.end () - 1)->c_str ())=‘S‘; cout << *(vs.end () - 1) << endl; vector<int> vi2 (10); print (vi2); vector<A> va (10); for (vector<A>::const_iterator it = va.begin (); it != va.end (); ++it) cout << it->m_i << ‘ ‘; cout << endl; vector<int> vi3 (10, 8); print (vi3); vector<A> va2 (10, A (8)); for (vector<A>::const_iterator it = va2.begin (); it != va2.end (); ++it) cout << it->m_i << ‘ ‘; cout << endl; return 0; }
标签:class priority 模板库 扩容 oid 访问 下标 hat ring
原文地址:https://www.cnblogs.com/xuxaut-558/p/10028408.html