C++/List/list insert

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

Combine insert and begin to add element to the start of 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
   std::ostream_iterator< int > output( cout, " " );
   // insert items in values
   values.push_front( 1 );
   values.push_front( 3 );
   values.push_back( 4 );
   values.push_back( 2 );
   cout << "values contains: ";
   std::copy( values.begin(), values.end(), output );
   values.insert( values.begin(), array, array + 4 );
   cout << "\n\nvalues contains: ";
   std::copy( values.begin(), values.end(), output );
   cout << endl;
   return 0;
}
/* 
values contains: 3 1 4 2
values contains: 2 6 4 8 3 1 4 2
 */


Combine insert and end to add elements to the end of a list

  
 
#include <iostream>
#include <cassert>
#include <algorithm>
#include <string>
#include <list>
#include <iostream>
using namespace std;
void print_list(string s)
{
  cout << s << endl;
}
int main()
{
  list<string> dlist;
  dlist.insert(dlist.end(), "AAA");
  dlist.insert(dlist.end(), "BBBB");
  dlist.insert(dlist.end(), "CCCCC");
  
  for_each(dlist.begin(), dlist.end(), print_list);
  return 0;
}
/* 
AAA
BBBB
CCCCC
 */


Fill list with random numbers with generate function

  
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
using namespace std;
int main( )
{
   list<int> data( 100000 );
   // create random numbers
   generate( data.begin(), data.end(), rand );
}


Initialize a list with values in a vector

  
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main( )
{
   short v[] = { 1, 2, 3, 4, 5, 6 };
   vector<short> v1( v,v + sizeof( v ) / sizeof( v[0] ) );
   vector<short> v2( v1 );

   list<short> l( v1.begin(), v1.end() );
   
   if( v1.size() == l.size() && equal( v1.begin(), v1.end(), l.begin() ) )
      cout << "The vector and list are equal" << endl;
   else
      cout << "The vector and list are not equal" << endl;
}


Insert elements of array into 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
   std::ostream_iterator< int > output( cout, " " );
   // insert items in values
   values.push_front( 1 );
   values.push_front( 3 );
   values.push_back( 4 );
   values.push_back( 2 );
   cout << "values contains: ";
   std::copy( values.begin(), values.end(), output );
   values.insert( values.begin(), array, array + 4 );
   cout << "\n\nvalues contains: ";
   std::copy( values.begin(), values.end(), output );
   cout << endl;
   return 0;
}
 /* 
values contains: 3 1 4 2
values contains: 2 6 4 8 3 1 4 2
 */


Move position pointer and insert again

  
#include <string>
#include <list>
#include <iostream>
using namespace std;
int main()
{  
   list<string> staff;
   staff.push_back("A");
   staff.push_back("B");
   staff.push_back("C");
   staff.push_back("D");
   list<string>::iterator pos;
   pos = staff.begin();
   pos++;
   pos++;
   pos++;
   staff.insert(pos, "E");
   pos = staff.begin();
   pos++;
   staff.erase(pos);
   for (pos = staff.begin(); pos != staff.end(); pos++)
      cout << *pos << "\n";
   return 0;
}