C++/Set Multiset/set

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

Add elements in a list to a set

  
 

#include <iostream>
#include <cassert>
#include <list>
#include <string>
#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
 */


Assigning sets to each other

  
#include <set>
#include <iostream>
#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);
}


Create a set that contains list1 - list2

  
#include <iostream>
#include <list>
#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;
}


Create sets with string elements

  
#include <set>
#include <iostream>
#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");
}


Declare a string set

  
 
#include <iostream>
#include <set>
#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
 */


Read keyboard input to a set directly

  
 
/* 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.
 */
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#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
 */


Use array to initialize a set

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <set>
#include <algorithm>
#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
 */