-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
STL #17
Comments
STL
Data Structures (container class)
Functions (algorithms) on the data structures
|
Categories of STL
Container Class
Iterator
Algorithm
|
Sequence Container Operations
|
Vector (Sequence Container class)
good at
declaration#include <vector>
vector<data_type> variable_name; // static allocation
vector<data_type>* variable_name = new vector<data_type>; // dynamic allocation member functions
examples
vector<int> first; // empty vector of ints
vector<int> x(10); // vector with size=10;
vector<int> second (4,100); // four ints (size=4) with value 100
vector<int> third (second.begin(),second.end()); // iterating through second
vector<int> fourth (third); // a copy of third
int myints[] = {16,2,77,29}; // using iterator constructor
vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
cout << "The contents of fifth are:";
for (i=0; i < fifth.size(); i++) // size function
cout << " " << fifth[i]; // operator[]
x[0]=7; // operator[]
x[1]=x[0]+5; vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i); // push_back function
vector<int>::iterator it;
cout << "myvector contains:";
for ( it=myvector.begin() ; it != myvector.end(); it++ ) // begin function, end function
cout << " " << *it; vector<int> myvector;
int sum (0);
myvector.push_back (100); // push_back function
myvector.push_back (200);
myvector.push_back (300);
while (!myvector.empty()) // empty function
{
sum+=myvector.back(); // back function
myvector.pop_back(); // pop_back function
} for (i=1; i<=10; i++) myvector.push_back(i); // push_back function
myvector.erase (myvector.begin()+5); // erase function, begin function // erasing 6th element
myvector.erase (myvector.begin(),myvector.begin()+3); // erasing first three elements |
Stack
stack<int> mystack;
for (int i = 0; i < 5; ++i)
mystack.push(i); // push function
cout << "Popping out elements...";
while (!mystack.empty()) // empty function
{
cout << " " << mystack.top(); // top function
mystack.pop(); // pop function
}
cout << endl; |
List
list<int> mylist (2,100); // two ints with a value of 100
mylist.push_front (200); // push_front function
mylist.push_front (300);
cout << "mylist contains:";
for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it) // begin function, end function
cout << " " << *it; |
Iterator
list<int>::iterator p;
vector<string>::iterator vp;
deque<double>::iterator dp; list<int> my_list, small, large;
list<int>::iterator ip;
for (int i = 1; i < 13; ++i)
{
my_list.push_back(i * i % 13); // push_back function
}
for (ip = my_list.begin(); ip != my_list.end(); ++ip) // =, begin function, !=. ++
{
if (*ip < 7) // *
{
small.push_back(*ip); // push_back function, *
}
else
{
large.push_back(*ip); // push_back function, *
}
} Benefits of iterators
template <class IteratorT, class T> // function template
IteratorT Find( IteratorT begin, IteratorT end, const T& value )
{
for ( ; begin != end ; begin++)
if (*begin == value) break;
return begin;
}
|
Algorithms
vector<T> A;
vector<T>::iterator p,q; sort()sort(p,q); // sort A between p and q
sort(A.begin(), A.end()); // sort all of A
find()vector<T> A;
vector<T>::iterator p,q;
find(p,q,search_value);
find(A.begin(), A.end(), search_value);
find_if()vector<T> A;
vector<T>::iterator p,q;
find_if(p, q, if_statement);
find_if(A.begin(), A.end(), if_statement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: