C++/List/list

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

access list

<source lang="cpp">

  1. include <set>
  2. include <string>
  3. include <list>
  4. include <iostream>
  5. 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);

}


 </source>


Add elements in a multiset to a list

<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 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"
*/        
   
 </source>


Add elements in a set to a list

<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;
 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
*/        
   
 </source>


Comparison Algorithms

<source lang="cpp">

  1. include <algorithm>
  2. include <vector>
  3. include <list>
  4. 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);

}


 </source>


Instantiating an STL List of Integers

<source lang="cpp">

  1. include <list>

int main () {

   using namespace std;
   list <int> listIntegers;
   return 0;

}


 </source>


Merge two lists.

<source lang="cpp">

  1. include <iostream>
  2. 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;

}


 </source>


Pass list to a function

<source lang="cpp">

  1. include <iostream>

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

  1. include <list> // list class-template definition
  2. include <algorithm> // copy algorithm
  3. 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

*/       
   
 </source>


Store class objects in a list

<source lang="cpp">

  1. include <iostream>
  2. include <list>
  3. 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

  • /
 </source>


Store class objects with overloaded operators in a list.

<source lang="cpp">

  1. include <iostream>
  2. include <list>
  3. 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;

}


 </source>


Use generic list to create a list of chars

<source lang="cpp">

  1. include <iostream>
  2. include <cassert>
  3. include <list>
  4. 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

  • /
 </source>


Use generic list to create list of strings

<source lang="cpp">

  1. include <iostream>
  2. include <cassert>
  3. include <list>
  4. 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

*/       
   
 </source>


Uses ostream_iterator and copy algorithm to output list elements

<source lang="cpp">

  1. include <iostream>

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

  1. include <list>
  2. include <algorithm>
  3. 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

*/        
   
 </source>


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

<source lang="cpp">

  1. include <iostream>

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

  1. include <list> // list class-template definition
  2. include <algorithm> // copy algorithm
  3. 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

*/       
   
 </source>


Using the list as a container for double value

<source lang="cpp">

  1. include <iostream>
  2. include <list>
  3. 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;

}


 </source>