C++/Vector/vector insert
Содержание
- 1 Appending values to Vector Containers after sorting
- 2 Combine insert and end to add elements to the end of vector
- 3 Create and initialize vector with float number array
- 4 Insert 10 duplicate value to vector
- 5 Insert characters into vector. An iterator to the inserted object is returned
- 6 Insert element at specific position
- 7 Insert element by index
- 8 Insert elements from array
- 9 Insert one vector to another vector
- 10 Inserts an element into a vector at given position
Appending values to Vector Containers after sorting
#include <algorithm>
#include <vector>
#include <iostream>
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( ){
const char* names1[] = { "A", "B", "C" };
const char* names2[] = { "D", "E", "F", "G" };
vector<string> squad1( names1,names1 + sizeof( names1 ) / sizeof( names1[0] ) );
vector<string> squad2( names2,names2 + sizeof( names2 ) / sizeof( names2[0] ) );
sort( squad1.begin(), squad1.end() );
print( squad1);
sort( squad2.begin(), squad2.end() );
print( squad2);
squad1.insert( squad1.end(), squad2.begin(), squad2.end() );
print( squad1);
sort( squad1.begin(), squad1.end() );
print( squad1);
}
Combine insert and end to add elements to the end of vector
#include <iostream>
#include <cassert>
#include <algorithm>
#include <string>
#include <list>
#include <deque>
#include <vector>
using namespace std;
int main()
{
vector<string> vec;
vec.insert(vec.end(), "AAA");
vec.insert(vec.end(), "BBBB");
vec.insert(vec.end(), "CCCCC");
vector<string>::iterator pos;
for (pos=vec.begin(); pos!=vec.end(); ++pos) {
cout << *pos << " ";
}
return 0;
}
/*
AAA BBBB CCCCC
*/
Create and initialize vector with float number array
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <vector>
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( ){
const float a[] = { 1, 1.3, 1.5, 0.9, 0.1, 0.2};
vector<float> data( a,a + sizeof( a ) / sizeof( a[0] ) );
cout << data.size() << " ELEMENTS\n";
print( data );
}
Insert 10 duplicate value to vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> v(10);
vector<char> v2;
char str[] = "<Vector>";
unsigned int i;
for(i=0; i<10; i++)
v[i] = i + "a";
for(i=0; str[i]; i++)
v2.push_back(str[i]);
cout << "Original contents of v:\n";
for(i=0; i<v.size(); i++)
cout << v[i] << " ";
cout << "\n\n";
vector<char>::iterator p = v.begin();
p += 2; // point to 3rd element
v.insert(p, 10, "X");
// display contents after insertion
cout << "Size after inserting X"s = " << v.size() << endl;
cout << "Contents after insert:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << "\n\n";
return 0;
}
/*
Original contents of v:
a b c d e f g h i j
Size after inserting X"s = 20
Contents after insert:
a b X X X X X X X X X X c d e f g h i j
*/
Insert characters into vector. An iterator to the inserted object is returned
#include <iostream>
#include <vector>
using namespace std;
void show(const char *msg, vector<char> vect);
int main() {
vector<char> v;
vector<char>::iterator itr;
itr = v.begin();
itr = v.insert(itr, "A");
itr = v.insert(itr, "B");
v.insert(itr, "C");
return 0;
}
void show(const char *msg, vector<char> vect) {
vector<char>::iterator itr;
cout << msg << endl;
for(itr=vect.begin(); itr != vect.end(); ++itr)
cout << *itr << endl;
}
Insert element at specific position
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;
void printVector(const vector<int>& v){
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
int main(int argc, char** argv){
vector<int> v1, v2;
int i;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(5);
// Insert it in the correct place
v1.insert(v1.begin() + 3, 4);
printVector(v1);
return (0);
}
Insert element by index
#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 );
// insert 22 as 2nd element
integers.insert( integers.begin() + 1, 22 );
cout << "\n\nVector integers contains: ";
std::copy( integers.begin(), integers.end(), output );
return 0;
}
/*
Vector integers contains: 1 2 3 4 5 6 2 3 4
Vector integers contains: 1 22 2 3 4 5 6 2 3 4
*/
Insert elements from array
#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 );
// insert elements from array
integers.insert( integers.begin(), array, array + 6 );
cout << "\n\nContents of vector integers before clear: ";
std::copy( integers.begin(), integers.end(), output );
return 0;
}
/*
Vector integers contains: 1 2 3 4 5 6 2 3 4
Contents of vector integers before clear: 1 2 3 4 5 6 1 2 3 4 5 6 2 3 4
*/
Insert one vector to another vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> v(10);
vector<char> v2;
char str[] = "<Vector>";
unsigned int i;
for(i=0; i<10; i++)
v[i] = i + "a";
for(i=0; str[i]; i++)
v2.push_back(str[i]);
cout << "Original contents of v:\n";
for(i=0; i<v.size(); i++)
cout << v[i] << " ";
cout << "\n\n";
vector<char>::iterator p = v.begin();
p += 2; // point to 3rd element
v.insert(p, 10, "X");
// display contents after insertion
cout << "Size after inserting X"s = " << v.size() << endl;
cout << "Contents after insert:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << "\n\n";
// Insert v2 into v
v.insert(p, v2.begin(), v2.end());
cout << "Size after v2"s insertion = ";
cout << v.size() << endl;
cout << "Contents after insert:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << endl;
return 0;
}
/*
Original contents of v:
a b c d e f g h i j
Size after inserting X"s = 20
Contents after insert:
a b X X X X X X X X X X c d e f g h i j
*/
Inserts an element into a vector at given position
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void insert(vector<string>& v, int pos, string s)
{
int last = v.size() - 1;
v.push_back(v[last]);
for (int i = last; i > pos; i--)
v[i] = v[i - 1];
v[pos] = s;
}
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 << "Insert before which element? ";
cin >> pos;
insert(staff, pos, "New, Nina");
print(staff);
return 0;
}