C++/List/list

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

access list

  
#include <set>
#include <string>
#include <list>
#include <iostream>
#include <iterator>
using namespace std;
using std::set;
using std::string;
using std::list;
class AccessList
{
 public:
  AccessList() {}
  void addUser(const string& user);
  void removeUser(const string& user);
  bool isAllowed(const string& user) const;
  list<string> getAllUsers() const;
 protected:
  set<string> mAllowed;
};
void AccessList::addUser(const string& user)
{
  mAllowed.insert(user);
}
void AccessList::removeUser(const string& user)
{
  mAllowed.erase(user);
}
bool AccessList::isAllowed(const string& user) const
{
  return (mAllowed.count(user) == 1);
}
list<string> AccessList::getAllUsers() const
{
  list<string> users;
  users.insert(users.end(), mAllowed.begin(), mAllowed.end());
  return (users);
}
int main(int argc, char** argv)
{
  AccessList fileX;
  fileX.addUser("A");
  fileX.addUser("B");
  fileX.addUser("C");
  fileX.removeUser("D");
  if (fileX.isAllowed("A")) {
    cout << "nsolter has permissions\n";
  }
  if (fileX.isAllowed("B")) {
    cout << "baduser has permissions\n";
  }
  list<string> users = fileX.getAllUsers();
  for (list<string>::const_iterator it = users.begin();
       it != users.end(); ++it) {
    cout << *it << " ";
  }
  cout << endl;
  return (0);
}


Add elements in a multiset to a list

  
 
#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 multiset1:
  multiset<char> multiset1;
  list<char>::iterator i;
  for (i = list1.begin(); i != list1.end(); ++i)
    multiset1.insert(*i);
  // Put the characters in multiset1 into list2:
  list<char> list2;
  multiset<char>::iterator k;
  for (k = multiset1.begin(); k != multiset1.end(); ++k)
    list2.push_back(*k);

  for (i = list2.begin(); i != list2.end(); ++i)
    cout << *i;
  return 0;
}
/* 
       ATaaaaccccdeeeehiiiiiiilllmmnnnnnorrrsssstttvy"
 */


Add elements in a set to a list

  
 
#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;
  list<char> list2;
  set<char>::iterator k;
  for (k = set1.begin(); k != set1.end(); ++k)
    list2.push_back(*k);

  for (i = list2.begin(); i != list2.end(); ++i)
    cout << *i;
  return 0;
}
/* 
 ATacdehilmnorstvy
 */


Comparison Algorithms

  
#include <algorithm>
#include <vector>
#include <list>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
  vector<int> myVector;
  list<int> myList;
  myVector.push_back(1);
  myVector.push_back(2);
  myVector.push_back(3);
  
  myList.push_back(1);
  myList.push_back(2);
  myList.push_back(3);
  
  if (myList.size() < myVector.size()) {
    cout << "Sorry, the list is not long enough.\n";
    return (0);
  }
  if (equal(myVector.begin(), myVector.end(), myList.begin())) {
    cout << "The two containers have equal elements\n";
  } 
  if (lexicographical_compare(myVector.begin(), myVector.end(), myList.begin(),myList.end())) {
    cout << "The vector is lexicographically first.\n";
  } else {
    cout << "The list is lexicographically first.\n";
  }
  return (0);
}


Instantiating an STL List of Integers

  
#include <list>
int main ()
{
    using namespace std;
    list <int> listIntegers;
    return 0;
}


Merge two lists.

  
#include <iostream>
#include <list>
using namespace std;
   
int main()
{
  list<int> lst1, lst2;
  int i;
   
  for(i=0; i<10; i+=2) lst1.push_back(i);
  for(i=1; i<11; i+=2) lst2.push_back(i);
   
  list<int>::iterator p = lst1.begin();
  while(p != lst1.end()) {
    cout << *p << endl;
    p++;
  }
   
  p = lst2.begin();
  while(p != lst2.end()) {
    cout << *p << endl;
    p++;
  }
   
  lst1.merge(lst2);
  if(lst2.empty())
    cout << "lst2 is now empty\n";
   
  cout << "Contents of lst1 after merge:\n";
  p = lst1.begin();
  while(p != lst1.end()) {
    cout << *p << " ";
    p++;
  }
   
  return 0;
}


Pass list to a function

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <list>      // list class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator
template < typename T > void printList( const std::list< T > &listRef );
int main()
{
   int array[ 4 ] = { 2, 6, 4, 8 };
   std::list< int > values;      // create list of ints
   std::list< int > otherValues; // create list of ints
   // insert items in values
   values.push_front( 1 );
   values.push_front( 2 );
   values.push_back( 4 );
   values.push_back( 3 );
   cout << "values contains: ";
   printList( values );
   cout << endl;
   return 0;
}
template < typename T > void printList( const std::list< T > &listRef )
{
    std::ostream_iterator< T > output( cout, " " );
    std::copy( listRef.begin(), listRef.end(), output );
}
 /* 
values contains: 2 1 4 3
 */


Store class objects in a list

  
 
#include <iostream>
#include <list>
#include <cstring>
using namespace std;
class MyClass {
  int a, b;
  int sum;
public:
  MyClass() {
      a = b = 0;
  }
  MyClass(int i, int j) {
    a = i;
    b = j;
    sum = a + b;
  }
  int getsum() {
      return sum;
  }
  friend bool operator<(const MyClass &o1,const MyClass &o2);
  friend bool operator>(const MyClass &o1,const MyClass &o2);
  friend bool operator==(const MyClass &o1,const MyClass &o2);
  friend bool operator!=(const MyClass &o1,const MyClass &o2);
};
bool operator<(const MyClass &o1, const MyClass &o2)
{
  return o1.sum < o2.sum;
}
bool operator>(const MyClass &o1, const MyClass &o2)
{
  return o1.sum > o2.sum;
}
bool operator==(const MyClass &o1, const MyClass &o2)
{
  return o1.sum == o2.sum;
}
bool operator!=(const MyClass &o1, const MyClass &o2)
{
  return o1.sum != o2.sum;
}
int main()
{
  list<MyClass> lst1;
  for(int i=0; i<10; i++) lst1.push_back(MyClass(i, i));
  cout << "First list: ";
  list<MyClass>::iterator p = lst1.begin();
  while(p != lst1.end()) {
    cout << p->getsum() << " ";
    p++;
  }
  cout << endl;
  // create a second list
  list<MyClass> lst2;
  for(int i=0; i<10; i++) lst2.push_back(MyClass(i*2, i*3));
  cout << "Second list: ";
  p = lst2.begin();
  while(p != lst2.end()) {
    cout << p->getsum() << " ";
    p++;
  }
  cout << endl;
  lst1.merge(lst2);
  cout << "Merged list: ";
  p = lst1.begin();
  while(p != lst1.end()) {
    cout << p->getsum() << " ";
    p++;
  }
  return 0;
}
/* 
First list: 0 2 4 6 8 10 12 14 16 18
Second list: 0 5 10 15 20 25 30 35 40 45
Merged list: 0 0 2 4 5 6 8 10 10 12 14 15 16 18 20 25 30 35 40 45 
*/


Store class objects with overloaded operators in a list.

  
#include <iostream>
#include <list>
#include <cstring>
using namespace std;
   
class myclass {
  int a, b;
  int sum;
public:
  myclass() { a = b = 0; }
  myclass(int i, int j) {
    a = i;
    b = j;
    sum = a + b;
  }
  int getsum() { return sum; }
   
  friend bool operator<(const myclass &o1,const myclass &o2);
  friend bool operator>(const myclass &o1,const myclass &o2);
  friend bool operator==(const myclass &o1,const myclass &o2);
  friend bool operator!=(const myclass &o1,const myclass &o2);
};
   
bool operator<(const myclass &o1, const myclass &o2)
{
  return o1.sum < o2.sum;
}
   
bool operator>(const myclass &o1, const myclass &o2)
{
  return o1.sum > o2.sum;
}
   
bool operator==(const myclass &o1, const myclass &o2)
{
  return o1.sum == o2.sum;
}
   
bool operator!=(const myclass &o1, const myclass &o2)
{
  return o1.sum != o2.sum;
}
   
int main()
{
  int i;
   
  list<myclass> lst1;
  for(i=0; i<10; i++) lst1.push_back(myclass(i, i));
   
  list<myclass>::iterator p = lst1.begin();
  while(p != lst1.end()) {
    cout << p->getsum() << endl;
    p++;
  }
   
  list<myclass> lst2;
  for(i=0; i<10; i++) lst2.push_back(myclass(i*2, i*3));
   
  p = lst2.begin();
  while(p != lst2.end()) {
    cout << p->getsum() << endl;
    p++;
  }
   
  // now, merget lst1 and lst2
  lst1.merge(lst2);
   
  p = lst1.begin();
  while(p != lst1.end()) {
    cout << p->getsum() << " ";
    p++;
  }
   
  return 0;
}


Use generic list to create a list of chars

  
 
#include <iostream>
#include <cassert>
#include <list>
#include <algorithm>  // For reverse
using namespace std;

int main()
{
  char x[5] = {"a", "b", "c", "d", "e"};
  list<char> list1(&x[0], &x[5]);

  reverse(list1.begin(), list1.end());
  list<char>::iterator i;
  cout.precision(10);
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << endl;
  cout << endl;

  return 0;
}
/* 
e
d
c
b
a
*/


Use generic list to create list of strings

  
 
#include <iostream>
#include <cassert>
#include <list>
#include <algorithm>  // For reverse
using namespace std;

int main()
{
  string x[5] = {"asdf", "1234", "2345", "6789", "0000"};
  list<string> list1(&x[0], &x[5]);

  reverse(list1.begin(), list1.end());
  list<string>::iterator i;
  cout.precision(10);
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << endl;
  cout << endl;

  return 0;
}
 /* 
0000
6789
2345
1234
asdf

 */


Uses ostream_iterator and copy algorithm to output list elements

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <list>      
#include <algorithm> 
#include <iterator>  
template < typename T > void printList( const std::list< T > &listRef );
int main()
{
   int array[ 4 ] = { 2, 6, 4, 8 };
   std::list< int > values;      
   std::list< int > otherValues; 
   values.push_front( 1 );
   values.push_front( 2 );
   values.push_back( 4 );
   values.push_back( 3 );
   cout << "values contains: ";
   printList( values );
   cout << endl;
   return 0;
}
template < typename T > void printList( const std::list< T > &listRef )
{
    std::ostream_iterator< T > output( cout, " " );
    std::copy( listRef.begin(), listRef.end(), output );
}
/* 
values contains: 2 1 4 3
 */


Use std::copy to print all elements in a list

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <list>      // list class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator
int main()
{
   int array[ 4 ] = { 2, 6, 4, 8 };
   std::list< int > values;      // create list of ints
   std::list< int > otherValues; // create list of ints
   // insert items in values
   values.push_front( 1 );
   values.push_front( 2 );
   values.push_back( 4 );
   values.push_back( 3 );
   cout << "values contains: ";
   std::ostream_iterator< int > output( cout, " " );
   std::copy( values.begin(), values.end(), output );
   cout << endl;
   return 0;
}
 /* 
values contains: 2 1 4 3
 */


Using the list as a container for double value

  
#include <iostream>
#include <list>
#include <numeric>
using namespace std;
void print(const list<double> &lst)
{                
   list<double>::const_iterator p;
   for (p = lst.begin();
        p !=lst.end(); ++p)
      cout << *p << endl;
   cout << endl;
}
int main()
{
   double w[4] = { 0.9, 0.8, 88, -99.99 };
   list<double> z;
   for( int i = 0; i < 4; ++i)
      z.push_front(w[i]);
   print(z);
   z.sort();
   print(z);
   cout << "sum is "
        << accumulate(z.begin(), z.end(), 0.0)
        << endl;
}