C++ STL笔记,迭代器,vector

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27605099/article/details/69230380

STL 中定义了5种迭代器:输入,输出,向前,双向,随机访问,所有迭代器都具备操作符==,!=,和解引用操作符*。

算法reverse:将线性表的y元素逆置。

reverse ( y.begin( ),y.end( ) );

算法accumulate:对线性表的y元素求和。

int sum = accumulate( y.begin ( ),y.end ( ),0 );

容器 vector:一个基于数组的类。

vector test 笔记:

main.cpp:
// test the class vectorList that uses a vector for the list elements
#include<iostream>
#include<numeric>
#include<algorithm>   // has reverse
#include<functional>  // has greater
#include "linearList.h"
#include "vectorList.h"


using namespace std;


int main()
{
   // test constructor
   linearList<double> *x = new vectorList<double>(20);
   vectorList<int> y(2), z;


   // test capacity
   cout << "Capacity of x, y and z = "
        << ((vectorList<double>*) x)->capacity() << ", "
        << y.capacity() << ", "
        << z.capacity() << endl;




   // test size
   cout << "Initial size of x, y, and z = "
        << x->size() << ", "
        << y.size() << ", "
        << z.size() << endl;


   // test empty
   if (x->empty()) cout << "x is empty" << endl;
   else cout << "x is not empty" << endl;
   if (y.empty()) cout << "y is empty" << endl;
   else cout << "y is not empty" << endl;


   // test insert
   y.insert(0, 2);
   y.insert(1, 6);
   y.insert(0, 1);
   y.insert(2, 4);
   y.insert(3, 5);
   y.insert(2, 3);
   cout << "Inserted 6 integers, list y should be 1 2 3 4 5 6" << endl;
   cout << "Size of y = " << y.size() << endl;
   cout << "Capacity of y = " << y.capacity() << endl;
   if (y.empty()) cout << "y is empty" << endl;
   else cout << "y is not empty" << endl;
   y.output(cout);
   cout << endl << "Testing overloaded <<" << endl;
   cout << y << endl;


   // test indexOf
   int index = y.indexOf(4);
   if (index < 0) cout << "4 not found" << endl;
   else cout << "The index of 4 is " << index << endl;


   index = y.indexOf(7);
   if (index < 0) cout << "7 not found" << endl;
   else cout << "The index of 7 is " << index << endl;


   // test get
   cout << "Element with index 0 is " << y.get(0) << endl;
   cout << "Element with index 3 is " << y.get(3) << endl;


   // test erase
   y.erase(1);
   cout << "Element 1 erased" << endl;
   cout << "The list is "  << y << endl;
   y.erase(2);
   cout << "Element 2 erased" << endl;
   cout << "The list is "  << y << endl;


   cout << "Size of y = " << y.size() << endl;
   cout << "Capacity of y = " << y.capacity() << endl;
   if (y.empty()) cout << "y is empty" << endl;
   else cout << "y is not empty" << endl;


   try {y.insert(-3, 0);}
   catch (illegalIndex e)
   {
      cout << "Illegal index exception" << endl;
      cout << "Insert index must be between 0 and list size" << endl;
      e.outputMessage();
   }


   // test copy constructor
   vectorList<int> w(y);
   y.erase(0);
   y.erase(0);
   cout << "w should be old y, new y has first 2 elements removed" << endl;
   cout << "w is " << w << endl;
   cout << "y is " << y << endl;
   
   // a few more inserts, just for fun
   y.insert(0,4);
   y.insert(0,5);
   y.insert(0,6);
   y.insert(0,7);
   cout << "y is " << y << endl;


   // test iterator
   cout << "Ouput using forward iterators pre and post ++" << endl;
   for (vectorList<int>::iterator i = y.begin();
        i != y.end(); i++)
      cout << *i << "  ";
   cout << endl;
   for (vectorList<int>::iterator i = y.begin();
        i != y.end(); ++i)
      cout << *i << "  ";
   cout << endl;


   cout << "Ouput using backward iterators pre and post --" << endl;
   for (vectorList<int>::iterator i = y.end();
        i != y.begin(); cout << *(--i) << "  ");
   cout << endl;
   for (vectorList<int>::iterator i = y.end();
        i != y.begin();)
      {i--; cout << *i << "  "; *i += 1;} 
   cout << endl;
   cout << "Incremented by 1 list is " << y << endl;
   
   // try out some STL algorithms
   reverse(y.begin(), y.end());
   cout << "The reversed list is " << y << endl;
   int sum = accumulate(y.begin(), y.end(), 0);
   cout << "The sum of the elements is " << sum << endl;
   sort(y.begin(), y.end());
   cout << "The sorted list is " << y << endl;
   sort(y.begin(), y.end(), greater<int>());
   cout << "The list is descending order is " << y << endl;
   return 0;
}



下面是vector.h:

#ifndef vectorList_
#define vectorList_


#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<algorithm>
#include<iterator>
#include "linearList.h"
#include "myExceptions.h"


using namespace std;


template<class T>
class vectorList : public linearList<T> 
{
   public:
      // constructor, copy constructor and destructor
      vectorList(int initialCapacity = 10);
      vectorList(const vectorList<T>&);
      ~vectorList() {delete element;}


      // ADT methods
      bool empty() const {return element->empty();}
      int size() const {return (int) element->size();}
      T& get(int theIndex) const;
      int indexOf(const T& theElement) const;
      void erase(int theIndex);
      void insert(int theIndex, const T& theElement);
      void output(ostream& out) const;


      // additional method
      int capacity() const {return (int) element->capacity();}
      
      // iterators to start and end of list
      typedef typename vector<T>::iterator iterator;
      iterator begin() {return element->begin();}
      iterator end() {return element->end();}


   protected:  // additional members of vectorList
      void checkIndex(int theIndex) const;
      vector<T>* element;     // vector to hold list elements
};


template<class T>
vectorList<T>::vectorList(int initialCapacity)
{// Constructor.
   if (initialCapacity < 1)
   {ostringstream s;
    s << "Initial capacity = " << initialCapacity << " Must be > 0";
    throw illegalParameterValue(s.str());
   }


   element = new vector<T>;
            // create an empty vector with capacity 0
   element->reserve(initialCapacity);
            // increase vector capacity from 0 to initialCapacity
}


template<class T>
vectorList<T>::vectorList(const vectorList<T>& theList)
{// Copy constructor.
   element = new vector<T>(*theList.element);
}


template<class T>
void vectorList<T>::checkIndex(int theIndex) const
{// Verify that theIndex is between 0 and size() - 1.
   if (theIndex < 0 || theIndex >= size())
   {ostringstream s;
    s << "index = " << theIndex << " size = " << size();
    throw illegalIndex(s.str());
   }


}


template<class T>
T& vectorList<T>::get(int theIndex) const
{// Return element whose index is theIndex.
 // Throw illegalIndex exception if no such element.
   checkIndex(theIndex);
   return (*element)[theIndex];
}


template<class T>
int vectorList<T>::indexOf(const T& theElement) const
{// Return index of first occurrence of theElement.
 // Return -1 if theElement not in list.


   // search for theElement
   int theIndex = (int) (find(element->begin(), element->end(),
                         theElement)
                         - element->begin());


   // check if theElement was found
   if (theIndex == size())
     // not found
     return -1;
   else return theIndex;
 }


template<class T>
void vectorList<T>::erase(int theIndex)
{// Delete the element whose index is theIndex.
 // Throw illegalIndex exception if no such element.
   checkIndex(theIndex);
   element->erase(begin() + theIndex);
}


template<class T>
void vectorList<T>::insert(int theIndex, const T& theElement)
{// Insert theElement so that its index is theIndex.
   if (theIndex < 0 || theIndex > size())
   {// invalid index
      ostringstream s;
      s << "index = " << theIndex << " size = " << size();
      throw illegalIndex(s.str());
   }


   element->insert(element->begin() + theIndex, theElement);
           // may throw an uncaught exception if insufficient
           // memory to resize vector
}


template<class T>
void vectorList<T>::output(ostream& out) const
{// Put the list into the stream out.
   copy(element->begin(), element->end(), ostream_iterator<T>(cout, "  "));
}


// overload <<
template <class T>
ostream& operator<<(ostream& out, const vectorList<T>& x)
   {x.output(out); return out;}


#endif




下面是LinearList.h


#ifndef linearList_
#define linearList_
#include <iostream>


using namespace std;


template<class T>
class linearList 
{
   public:
      virtual ~linearList() {};
      virtual bool empty() const = 0;
                  // return true iff list is empty
      virtual int size() const = 0;
                  // return number of elements in list
      virtual T& get(int theIndex) const = 0;
                  // return element whose index is theIndex
      virtual int indexOf(const T& theElement) const = 0;
                  // return index of first occurence of theElement
      virtual void erase(int theIndex) = 0;
                  // remove the element whose index is theIndex
      virtual void insert(int theIndex, const T& theElement) = 0;
                  // insert theElement so that its index is theIndex
      virtual void output(ostream& out) const = 0;
                  // insert list into stream out
};
#endif







猜你喜欢

转载自blog.csdn.net/qq_27605099/article/details/69230380