C++/Set Multiset/set

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Add elements in a list to a set

<source lang="cpp">


  1. include <iostream>
  2. include <cassert>
  3. include <list>
  4. include <string>
  5. include <set>

using namespace std; int main() {

 string s("There is no distinctly native American criminal class");
 list<char> list1(s.begin(), s.end());
 // Put the characters in list1 into set1:
 set<char> set1;
 list<char>::iterator i;
 for (i = list1.begin(); i != list1.end(); ++i)
   set1.insert(*i);
 set<char>::iterator j;
 for (j = set1.begin(); j != set1.end(); ++j)
   cout << *j;
 return 0;

} /*

ATacdehilmnorstvy
*/        
   
 </source>


Assigning sets to each other

<source lang="cpp">

  1. include <set>
  2. include <iostream>
  3. include <string>

using namespace std; template <class T> void print(T& c){

  for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
     std::cout << *i << endl;
  }

} int main() {

 set<string> first;
 first.insert("r");
 first.insert("T");
 first.insert("s");
 cout << first.size() << endl;
 set<string> second (first);   // Copy constructor
 second.insert("r");
 second.insert("K");
 second.insert("S");
 second.insert("b");
 cout << second.size() << endl;
 set<string> third = first;
 print(first);
 print(second);
 print(third);
 set<string> set2(first);
 set2.insert("l");
 
 print(set2);

}


 </source>


Create a set that contains list1 - list2

<source lang="cpp">

  1. include <iostream>
  2. include <list>
  3. include <algorithm>

using namespace std; template<class InIter> void show_range(const char *msg, InIter start, InIter end); int main() {

 list<char> list1, list2, result(15);
 list<char>::iterator res_end;
 for(int i=0; i < 5; i++) {
    list1.push_back("A"+i);
 }
 for(int i=3; i < 10; i++) {
    list2.push_back("A"+i);
 }
 show_range("Contents of list1: ", list1.begin(), list1.end());
 show_range("Contents of list2: ", list2.begin(), list2.end());
 res_end = set_difference(list1.begin(), list1.end(),list2.begin(), list2.end(),result.begin());
 show_range("list1 - list2: ", result.begin(), res_end);
 return 0;

} template<class InIter> void show_range(const char *msg, InIter start, InIter end) {

 InIter itr;
 cout << msg << endl;
 for(itr = start; itr != end; ++itr)
   cout << *itr << endl;

}


 </source>


Create sets with string elements

<source lang="cpp">

  1. include <set>
  2. include <iostream>
  3. include <string>

using namespace std; template <class T> void print(T& c){

  for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
     std::cout << *i << endl;
  }

} int main() {

 set<string> first;
 first.insert("r");
 first.insert("T");
 first.insert("s");

}


 </source>


Declare a string set

<source lang="cpp">

  1. include <iostream>
  2. include <set>
  3. include <string>

using namespace std; int main( ) {

  set<string> setStr;
  string s = "B";
  setStr.insert(s);
  s = "S";
  setStr.insert(s);
  s = "R";
  setStr.insert(s);
  s = "H";
  setStr.insert(s);
  for (set<string>::const_iterator p = setStr.begin( );p != setStr.end( ); ++p)
     cout << *p << endl;

} /* B H R S

*/        
   
 </source>


Read keyboard input to a set directly

<source lang="cpp">

/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <string>
  3. include <algorithm>
  4. include <iterator>
  5. include <set>

using namespace std; int main() {

   /* create a string set
    * - initialized by all words from standard input
    */
   set<string> coll((istream_iterator<string>(cin)),
                    istream_iterator<string>());
   // print all elements
   copy (coll.begin(), coll.end(),
         ostream_iterator<string>(cout, "\n"));

} /* a b cd ^CTerminate batch job (Y/N)? n

*/        
   
 </source>


Use array to initialize a set

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. include <set>
  2. include <algorithm>
  3. include <iterator> // ostream_iterator

int main() {

  double a[ 5 ] = { 2.1, 4.2, 9.5, 2.1, 3.7 };
  std::set< double, std::less< double > > doubleSet( a, a + 5 );;
  std::ostream_iterator< double > output( cout, " " );
  cout << "doubleSet contains: ";
  std::copy( doubleSet.begin(), doubleSet.end(), output );
  cout << endl;
  return 0;

} /* doubleSet contains: 2.1 3.7 4.2 9.5

*/        
   
 </source>