C++/Vector/vector erase

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

Erase first element

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <vector>    // vector class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator iterator
#include <stdexcept> // out_of_range exception
int main()
{
   int array[ 6 ] = { 1, 2, 3, 4, 5, 6 };
   std::vector< int > integers( array, array + 6 );
   std::ostream_iterator< int > output( cout, " " );
   integers.push_back( 2 );
   integers.push_back( 3 );
   integers.push_back( 4 );
   cout << "Vector integers contains: ";
   std::copy( integers.begin(), integers.end(), output );
   // erase first element
   integers.erase( integers.begin() );
   cout << "\n\nVector integers after erasing first element: ";
   std::copy( integers.begin(), integers.end(), output );

   return 0;
}
/* 
Vector integers contains: 1 2 3 4 5 6 2 3 4
Vector integers after erasing first element: 2 3 4 5 6 2 3 4 
 */


Remove(delete) all elements in the vector

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <vector>    // vector class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator iterator
#include <stdexcept> // out_of_range exception
int main()
{
   int array[ 6 ] = { 1, 2, 3, 4, 5, 6 };
   std::vector< int > integers( array, array + 6 );
   std::ostream_iterator< int > output( cout, " " );
   integers.push_back( 2 );
   integers.push_back( 3 );
   integers.push_back( 4 );
   cout << "Vector integers contains: ";
   std::copy( integers.begin(), integers.end(), output );
   // erase remaining elements
   integers.erase( integers.begin(), integers.end() );
   cout << "\nAfter erasing all elements, vector integers "
      << ( integers.empty() ? "is" : "is not" ) << " empty";
   return 0;
}
/* 
Vector integers contains: 1 2 3 4 5 6 2 3 4
After erasing all elements, vector integers is empty
 */


Removes an element from an ordered vector

  
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void erase(vector<string>& v, int pos)
{ 
   for (int i = pos; i < v.size() - 1; i++)
      v[i] = v[i + 1];
   v.pop_back();
}
void print(vector<string> v){  
   for (int i = 0; i < v.size(); i++)
      cout << "[" << i << "] " << v[i] << "\n";
}
int main()
{  
   vector<string> staff(5);
   staff[0] = "A";
   staff[1] = "B";
   staff[2] = "C";
   staff[3] = "D";
   staff[4] = "E";
   print(staff);
   int pos;
   cout << "Remove which element? ";
   cin >> pos;
   erase(staff, pos);
   print(staff);
   return 0;
}


Removes an element from an unordered vector

  
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void erase(vector<string>& v, int pos)
{  
   int last_pos = v.size() - 1;
   v[pos] = v[last_pos];
   v.pop_back();
}
void print(vector<string> v){  
   for (int i = 0; i < v.size(); i++)
      cout << "[" << i << "] " << v[i] << "\n";
}
int main()
{  
   vector<string> staff(5);
   staff[0] = "A";
   staff[1] = "B";
   staff[2] = "C";
   staff[3] = "D";
   staff[4] = "E";
   print(staff);
   int pos;
   cout << "Remove which element? ";
   cin >> pos;
   erase(staff, pos);
   print(staff);
   return 0;
}


Use unique_copy to remove duplicate words

  
 
/* 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 <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
    vector<string> coll;
    /* read all words from the standard input
     * - source: all strings until end-of-file (or error)
     * - destination: coll (inserting)
     */
    copy (istream_iterator<string>(cin),    // start of source
          istream_iterator<string>(),       // end of source
          back_inserter(coll));             // destination
    // sort elements
    sort (coll.begin(), coll.end());
    /* print all elements without duplicates
     * - source: coll
     * - destination: standard output (with newline between elements)
     */
    unique_copy (coll.begin(), coll.end(),             // source
                 ostream_iterator<string>(cout,"\n")); // destination
}
 /* 
asdf
as
asd
as
d
asd
as
a^CTerminate batch job (Y/N)? n
 */