Swap second and fourth element in a vector
2010-12-17 10:24
260 查看
/* 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()
{
// create empty vector for strings
vector<string> sentence;
// reserve memory for five elements to avoid reallocation
sentence.reserve(5);
// append some elements
sentence.push_back("Hello,");
sentence.push_back("how");
sentence.push_back("are");
sentence.push_back("you");
sentence.push_back("?");
// swap second and fourth element
swap (sentence[1], sentence[3]);
// print elements separated with spaces
copy (sentence.begin(), sentence.end(),
ostream_iterator<string>(cout," "));
cout << endl;
}
* "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()
{
// create empty vector for strings
vector<string> sentence;
// reserve memory for five elements to avoid reallocation
sentence.reserve(5);
// append some elements
sentence.push_back("Hello,");
sentence.push_back("how");
sentence.push_back("are");
sentence.push_back("you");
sentence.push_back("?");
// swap second and fourth element
swap (sentence[1], sentence[3]);
// print elements separated with spaces
copy (sentence.begin(), sentence.end(),
ostream_iterator<string>(cout," "));
cout << endl;
}
相关文章推荐
- some different between array and Vector(C++) when trying to swap two element
- find element in both array and vector using one myfind function
- Swap kth element from the beginning and kth element from the end of linked list -- Amazon
- 《程序员面试题精选》02.Stack and minimum element in O(1)
- 215. Kth Largest Element in an Array(divide and conquer)
- color vector using in plotting example points and lines between corresponding vertices
- Divide and Conquer-215-Kth Largest Element in an Array
- Leetcode_swap-nodes-in-pairs(c++ and python version)
- 【算法】【Divide and conquer】Kth Largest Element in an Array
- [TS] Swap two element in the array (mutation)
- 第六章 Vector Shapes and Text in 3D
- Data Structures and Algorithm Analysis in C, Second Edition(《数据结构与算法分析》C语言版 第二版)——Mark Allen Weiss
- leetCode刷题归纳-Divide and Conquer(215. Kth Largest Element in an Array)
- Cisco 2600 and 3600 Stay in ROM Monitor After Second Software Failure
- The Ruby Way, Second Edition: Solutions and Techniques in Ruby, Programming (Paperback) 2nd.Edition.Oct.2006.eBook-BBL
- Find the second largest element in a Binary Search Tree
- 随笔- Get Root Element and namespace of xml in mssql
- Difference between ArrayList and Vector In java
- LeetCode OJ|Divide and Conquer|Kth Largest Element in an Array
- leetcode_c++:Divide and Conquer:Kth Largest Element in an Array(215)