#include <iostream>
#include <vector>
#include <deque>
#include <iterator>
int main()
{
std::cout<<std::endl<<"Creating vector and filling with values 0-9.";
std::vector<int> intVector;
for (int i = 0; i < 10; ++i)
intVector.push_back(i);
std::cout<<std::endl<<std::endl<<"Using copy function to output vector contents:\n";
std::copy(intVector.begin(), intVector.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl<<"Creating a deque and using copy with back_inserter to populate it with vector contents back-first.";
std::deque<int> intDeque;
std::copy(intVector.begin(), intVector.end(), back_inserter(intDeque));
std::cout<<std::endl<<std::endl<<"Using copy with front_inserter to insert vector data to the deque again front-first.";
std::copy(intVector.begin(), intVector.end(), front_inserter(intDeque));
std::cout<<std::endl<<std::endl<<"Outputting the contents of the new deque with copy, should be 9..0..0..9:\n";
std::copy(intDeque.begin(), intDeque.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl; //Formatting
}
#include <iostream>
#include <map>
#include <iterator>
namespace std // extend the std namespace (the magic of ADL means this will be used by copy)
{
template<typename K, typename V>
ostream & operator << (ostream & os, std::pair<K, V> const & rhs)
{
return os << rhs.first << " => " << boolalpha << rhs.second;
}
}
int main()
{
std::cout<<"Creating myMap as map<int, bool> and filling with {key: int 0-9, value: key%3 > 0 -->bool}.\n";
std::map<int, bool> myMap;
for (int i = 0; i < 10; ++i)
myMap.insert(std::make_pair(i, i%3 > 0));
std::cout<<std::endl<<"Creating myOtherMap and swapping its empty data with myMap using std::swap(map1, map2)...\n";
std::map<int, bool> myOtherMap;
std::swap(myMap, myOtherMap);
std::cout<<std::endl<<"Outputting contents of MyMap:\n";
std::copy(myMap.begin(), myMap.end(), std::ostream_iterator<std::pair<int, bool> >(std::cout, "\n"));
std::cout<<std::endl<<"Outputting contents of MyOtherMap:\n";
std::copy(myOtherMap.begin(), myOtherMap.end(), std::ostream_iterator<std::pair<int, bool> >(std::cout, "\n"));
std::cout << std::endl << "Swapping myMap data back into myOtherMap using std::map::swap(map2)...\n";
myOtherMap.swap(myMap);
std::cout<<std::endl<<"Outputting contents of MyMap:\n";
std::copy(myMap.begin(), myMap.end(), std::ostream_iterator<std::pair<int, bool> >(std::cout, "\n"));
std::cout<<std::endl<<"Outputting contents of MyOtherMap:\n";
std::copy(myOtherMap.begin(), myOtherMap.end(), std::ostream_iterator<std::pair<int, bool> >(std::cout, "\n"));
std::cout<<std::endl<<std::endl; //Formatting
}
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
//Functor for the transform_example.
class float_div7_plus2 {
public:
float operator()(float val) {
return val/7+2;
}
};
int main()
{
std::list<float> floatList;
for (int i = 2; i <= 16; i+=2)
floatList.push_back((float)i);
std::cout<<"Displaying values held in list before transform:\n";
std::copy(floatList.begin(), floatList.end(), std::ostream_iterator<float>(std::cout, " "));
std::cout<<std::endl<<std::endl<<"Transforming numbers by multiplying them by themselves (squaring them).\n";
std::transform(floatList.begin(), floatList.end(), floatList.begin(), floatList.begin(), std::multiplies<float>());
std::cout<<"Displaying values held in list after transform squaring operation:\n";
std::copy(floatList.begin(), floatList.end(), std::ostream_iterator<float>(std::cout, " "));
std::cout<<std::endl<<std::endl<<"Transforming numbers using custom function to divide by 7 and add 2.\n";
std::transform(floatList.begin(), floatList.end(), floatList.begin(), float_div7_plus2());
std::cout<<"Displaying values held in list after transform squaring operation:\n";
std::copy(floatList.begin(), floatList.end(), std::ostream_iterator<float>(std::cout, " "));
std::cout<<std::endl<<std::endl; //Formatting
}
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
//Binary predicate to compare numbers by the value of their last digit.
bool SortByLastDigit(const int& lhs, const int& rhs) {
return lhs%10 < rhs%10;
}
int main()
{
std::cout<<"Creating a vector of integers that are not properly sorted using (i%3 + (i+2)).\n";
std::vector<int> intList;
for (int i = 0; i < 10; ++i)
intList.push_back(i%3 + (i+2));
std::cout<<"Displaying values held in vector before sort:\n";
std::copy(intList.begin(), intList.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl<<"Sorting numbers without providing binary predicate --> uses default of <.\n";
std::sort(intList.begin(), intList.end());
std::cout<<"Displaying sorted results:\n";
std::copy(intList.begin(), intList.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl<<"Sorting contents of vector using SortByLastDigit binary predicate function.\n";
std::sort(intList.begin(), intList.end(), SortByLastDigit);
std::cout<<"Displaying values of list after SortByLastDigit sort:\n";
std::copy(intList.begin(), intList.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl; //Formatting
}
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
int main()
{
std::cout<<"Creating intList with values 0-9.\n";
std::list<int> intList;
for (int i = 0; i < 10; ++i)
intList.push_back(i);
std::cout<<"Displaying values held in list before modification:\n";
std::copy(intList.begin(), intList.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl<<"Using find_if with unary predicate to display odd list items.\n";
while (true) {
std::list<int>::iterator found = std::find_if(intList.begin(), intList.end(), std::bind2nd(std::modulus<int>(), 2));
if (found != intList.end()) {
std::cout<<"Found even #: " << *found <<". --> Removing it now"<<std::endl;
intList.remove(*found);
}
else
break;
}
std::cout<<std::endl<<"Displaying values held in list after modification:\n";
std::copy(intList.begin(), intList.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl<<"Replacing values that are divisible by 4 with 99.\n";
std::replace_if(intList.begin(), intList.end(), std::bind2nd(std::modulus<int>(), 4), 99);
std::cout<<"Displaying values held in list after modification:\n";
std::copy(intList.begin(), intList.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl;
}
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
int main()
{
std::cout<<"Using generate to populate list with 10 random numbers.";
std::list<int> intList;
std::generate_n(back_inserter(intList), 10, rand);
std::cout<<std::endl<<std::endl<<"Displaying values held in list before modification:\n";
std::copy(intList.begin(), intList.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl<<"Sorting list contents to show reverse easier.";
intList.sort();
std::cout<<std::endl<<std::endl<<"Displaying values held in list after sort:\n";
std::copy(intList.begin(), intList.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl<<"Calling reverse and displaying list after modification:\n";
std::reverse(intList.begin(), intList.end());
std::copy(intList.begin(), intList.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl<<std::endl; //Formatting
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Open in new window
And it's not good to use std::endl instead of '\n' just to use some different thing.The '\n' and std::endl differs so that std::endl not only prints '\n' char, it flushs a stream buffer else.
If you aware that program can suddenly crash, you should write std::endl to ensure that buffer will be flushed, and text will be written to stdout just after writing statement (you write "text\n"). But if you just srart to write a text, why do you flush a buffer? Not just flush it, you flush it twice!.
If you want to separate leading '\n' from string, you can just write
Open in new window
I think it's more readable, and it won't do useless work.Commented:
Whilst your point is valid it's not really the point of the article and is, thus, really unimportant in the context of what this article is actually discussing.