Difference between revisions of "CPP/STL/Set"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==Set.cpp== <source lang="cpp"> #include <iostream> #include <set> #include <vector> #include <cstdlib> #include "Height.h" double RandomDouble(); void Report(std::set<double>…') |
Daviddoria (Talk | contribs) |
||
Line 6: | Line 6: | ||
#include <cstdlib> | #include <cstdlib> | ||
− | #include | + | #include <algorithm> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
int main(int argc, char* argv[]) | int main(int argc, char* argv[]) | ||
{ | { | ||
− | // | + | // Create a set |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
std::set<unsigned int> S; | std::set<unsigned int> S; | ||
− | // | + | // Add 10 elements to the set |
− | + | ||
− | + | ||
− | + | ||
for(unsigned int i = 0; i < 10; i++) | for(unsigned int i = 0; i < 10; i++) | ||
{ | { | ||
S.insert(i); | S.insert(i); | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | // Output all of the elements in the set | |
− | + | for(std::set<unsigned int>::iterator it1 = S.begin(); it1 != S.end(); it1++) | |
− | // | + | |
− | std::set< | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
{ | { | ||
− | + | std::cout << " " << *it1; | |
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | std::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl; | |
− | std:: | + | |
− | + | ||
− | + | ||
− | + | return 0; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | + | </source> | |
− | + | ||
− | + | ||
− | + | ==CMakeLists.txt== | |
− | + | <source lang="cmake"> | |
− | + | cmake_minimum_required(VERSION 2.6) | |
− | + | ||
− | + | ||
− | + | Project(Set) | |
− | + | ||
− | + | ADD_EXECUTABLE(Set Set.cpp) | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> |
Revision as of 10:21, 30 January 2011
Set.cpp
#include <iostream> #include <set> #include <vector> #include <cstdlib> #include <algorithm> int main(int argc, char* argv[]) { // Create a set std::set<unsigned int> S; // Add 10 elements to the set for(unsigned int i = 0; i < 10; i++) { S.insert(i); } // Output all of the elements in the set for(std::set<unsigned int>::iterator it1 = S.begin(); it1 != S.end(); it1++) { std::cout << " " << *it1; } std::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl; return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) Project(Set) ADD_EXECUTABLE(Set Set.cpp)