精选文章

Android下使用TCPDUMP抓包Wireshark分析数据 如果想分析Android下某个APP的网络数据交互,需要在Android手机上抓包,最常用的抓包工具非tcpdump莫属,用tcpdump生成Wireshark识别的pcap文件,然后将pcap文件下载到电脑上,用电脑上的Wireshark加载pcap文件,通过Wireshark分析tcpdump抓取的数据。...

继续阅读

Mac下部署Android开发环境附加NDK 作为开发者,我们深有体会,不管是进行什么开发,为了部署开发环境,我们往往需要折腾很长时间、查阅很多资料才能完成,而且这次折腾完了,下次到了另一台新电脑上又得重新来过,整个部署过程记得还好,要是不记得又得重新开始,而且遇到Android这种GFW阻隔了开发资源下载链接的环境部署,又尤其浪费时间。所以这也是我写下这篇教程的初衷跟动力源泉,希望大家参考了这篇教程以后可以轻轻松松在Mac系统下将Android环境部署好。...

继续阅读

稍顯嚴肅的台中 坦白說,留在腦海中的台中影像並不多,來台灣之前在Booking上只訂到了台中的一家青旅,第一次住青旅有些不習慣,幹什麼都放不開。 同屋的一個男生是台灣人,不過一年中四分之三的時間在上海跟北京,這麼說來跟我還是比較有共同話題的。得之我準備花15天的時間環島,覺得太倉促了,他們大學時期花一個半月的時間也不見得能將台灣島給逛完。我只能無奈地表示,兩岸允許的簽證時間有限,自己的空閒時間更有限,只能用打卡式的旅行了,我深知正真地旅行應該慢下來,融入當地的環境,感受他們的風土人情,但第一次只能這樣作罷,以後換成民進黨上台,形勢會變成怎樣還不得而知,能否再過來還是個未知數。而我一向信奉的人生格言是秉燭夜遊,活在當下,所以,理解自己吧。...

继续阅读

為之留戀的新竹 來新竹之前本沒有對她有過高的期待,慢慢對她加分要從桃園火車站出發前往新竹開始。 在桃園火車站的候車月台上,有醒目的旅遊資料發放處,這上面的擺放的全是新竹的旅遊宣傳資料,關鍵的是資料做得非常簡潔易懂,而接下來一天的新竹之行就全部是依據這份寶典的指引來完成的。...

继续阅读

從桃園開始台灣之行 初到台灣恰逢華夏銀行系統升級,特意準備的華夏銀聯卡在桃園機場沒能派上用場,只好用建行在機場5000塊,算下來是很不划算的,但是沒辦法,誰叫我出機場就得花錢呢。 從機場打車到桃園的酒店,花了將近六百塊新台幣,到酒店時五點多,天已經漸亮了,洗漱完等到七點吃過早餐就開始補覺囉,一覺醒來已是中午,帶著換下來的衣服外出找自助洗衣店,順便覓食。...

继续阅读

  • Prev
  • Next

变序算法实例二

3

文章分类 : C++, 泛型算法

变序列算法属于C++泛型算法中的一种,需要修改容器内容。这些算法包括copy_backward, fill, generate, partition, random_shuffle, remove, replace, rotate, reverse, swap, swap_ranges, transform, unique。上一篇《变序算法实例一》介绍了copy_backward, fill, generate, partition, random_shuffle, remove,这一篇接着介绍replace, rotate, reverse, swap, swap_ranges, transform, unique。

11、replace_copy——将指定区间内的指定值替全部换为另一值后拷贝入另一容器,原区间值不变

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std; 

void main()
{
    const int MAX_ELEMENTS = 8 ; 

    typedef vector<int > IntVector ; 
    typedef IntVector::iterator IntVectorIt ; 
    IntVector Numbers(MAX_ELEMENTS),Result(MAX_ELEMENTS);
    IntVectorIt start, end, it, last, resultIt;

    Numbers[0] = 10;
    Numbers[1] = 20;
    Numbers[2] = 10;
    Numbers[3] = 15;
    Numbers[4] = 12;
    Numbers[5] = 7;
    Numbers[6] = 9;
    Numbers[7] = 10; 

    start = Numbers.begin() ;
    end = Numbers.end() ; 
    resultIt = Result.begin() ;
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ; 

    last = replace_copy(start, end, resultIt, 10, 30) ; 

    cout << "Total number of elements copied to Result = "
        << last - resultIt << endl ;
    start = Result.begin() ;
    end = Result.end() ;
    cout << "Result  { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
}
运行结果:
//  Numbers { 10 20 10 15 12 7 9 10  }
//  Total number of elements copied to Result = 8
//  Result  { 30 20 30 15 12 7 9 30  }

12、replace_copy_if——将指定区间内满足指定条件的值替全部换为另一值后拷贝入另一容器,原区间值不变

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std; 

void main()
{
    const int MAX_ELEMENTS = 8 ; 
    typedef vector<int> IntVector ; 
    typedef IntVector::iterator IntVectorIt ; 
    IntVector Numbers(MAX_ELEMENTS),Result(MAX_ELEMENTS);
    IntVectorIt start, end, it, last, resultIt; 

    Numbers[0] = 10 ;
    Numbers[1] = 20 ;
    Numbers[2] = 10 ;
    Numbers[3] = 15 ;
    Numbers[4] = 12 ;
    Numbers[5] = 7 ;
    Numbers[6] = 9 ;
    Numbers[7] = 10 ; 

    start = Numbers.begin() ; 
    end = Numbers.end() ;
    resultIt = Result.begin() ;

    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ; 

    // 将Number中大于等于10的元素替换为30以后再拷贝到result中,result中的值保持不变
    last = replace_copy_if(start, end, resultIt,
                bind2nd(greater_equal<int>(), 10), 30); 
    cout << "Total number of elements copied to Result = "
        << last - resultIt << endl ; 

    start = Result.begin() ; 
    end = Result.end() ; 
    cout << "Result  { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
}
运行结果:
//  Numbers { 10 20 10 15 12 7 9 10  }
//  Total number of elements copied to Result = 8
//  Result  { 30 30 30 30 30 7 9 30  }

13、replace_if——将指定区间内满足指定条件的值替全部换为另一值后

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std; 

void main()
{
    const int VECTOR_SIZE = 8 ; 
    typedef vector<int > IntVector ; 
    typedef IntVector::iterator IntVectorIt ;
    IntVector Numbers(VECTOR_SIZE);
    IntVectorIt start, end, it ;

    start = Numbers.begin() ;
    end = Numbers.end() ;

    Numbers[0] = 10 ;
    Numbers[1] = 20 ;
    Numbers[2] = 10 ;
    Numbers[3] = 15 ;
    Numbers[4] = 12 ;
    Numbers[5] = 7 ;
    Numbers[6] = 9 ;
    Numbers[7] = 10 ; 

    cout << "Before calling replace_if" << endl ; 
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ; 

    // 将所有小于等于10的元素替换为4
    replace_if(start, end,
               bind2nd(less_equal<int>(), 10), 4 ) ;
    cout << "After calling replace_if" << endl ; 
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
}
运行结果:
//  Before calling replace_if
//  Numbers { 10 20 10 15 12 7 9 10  }
//  After calling replace_if
//  Numbers { 4 20 4 15 12 4 4 4  }

14、rotate——容器元素整体移动,使得指定元素成为容器头

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std; 

int main()
{
    vector v(10); 

    iota(v.begin(),v.end(),1); 

    copy(v.begin(),v.end(),
        ostream_iterator<int>(cout," "));
       cout << endl;

    //  整体移位使得第二个元素成为第一个
    rotate (v.begin(),      // 容器头
            v.begin() + 1,  // 新的容器头
            v.end());       // 容器尾

    cout << "one left:  ";
    copy(v.begin(),v.end(),
        ostream_iterator<int>(cout," "));
       cout << endl; 

    // 整体移位使得倒数第二个元素成为第一个
    rotate (v.begin(),  // 容器头
            v.end() - 2,  // 新的容器头
            v.end());     // 容器尾
    cout << "two right: ";
    copy(v.begin(),v.end(),
        ostream_iterator<int>(cout," "));
       cout << endl; 

    // 整体移位使得值为4的元素成为第一个
    rotate (v.begin(),     // 容器头
            find(v.begin(),v.end(),4), // 新的容器头
            v.end());   // 容器尾
    cout << "4 first:   ";
    copy(v.begin(),v.end(),
        ostream_iterator<int>(cout," "));
       cout << endl; 

       return 0;
}
运行结果:
// 1 2 3 4 5 6 7 8 9 10
// one left:  2 3 4 5 6 7 8 9 10 1
// two right: 10 1 2 3 4 5 6 7 8 9
// 4 first:   4 5 6 7 8 9 10 1 2 3

15、rotate_copy——容器元素整体移动以后存入指定的目标地址,源容器不变

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std; 

int main()
{
    vector<int> v(10);
    iota(v.begin(),v.end(),1); 

    copy(v.begin(),v.end(),
        ostream_iterator<int>(cout," "));
       cout << endl; 

    // 左移一位并打印出来
    vector<int>::iterator pos = v.begin();
    advance(pos,1); //pos += 1;
    rotate_copy(v.begin(),  // 源容器头
                pos,           // 新的第一元素
                v.end(),       // 源容器尾
        ostream_iterator<int>(cout," "));
                               // 目的地址
    cout << endl;

    // 右移一位并打印出来
    pos = v.end();
    advance(pos,-2);
    rotate_copy(v.begin(), // 源容器头
                pos,           // 新的第一元素
                v.end(),       // 源容器尾
        ostream_iterator<int>(cout," "));
                              // 目的地址
    cout << endl; 

    return 0;
}
运行结果:
// 1 2 3 4 5 6 7 8 9 10
// 2 3 4 5 6 7 8 9 10 1
// 9 10 1 2 3 4 5 6 7 8

16、reverse或reverse_copy——容器元素本身顺序反向(或)容器自身顺序不变而反向拷贝到指定的目标地址

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std; 

int main()
{
    vector<int> v(5);
    iota(v.begin(),v.end(),1);
    copy(v.begin(),v.end(),
        ostream_iterator<int>(cout," "));
       cout << endl;

    // 使元素顺序反向
    reverse (v.begin(), v.end());
    copy(v.begin(),v.end(),
        ostream_iterator<int>(cout," "));
       cout << endl; 

    // 使第二个至倒数第二个元素顺序反向
    reverse (v.begin()+1, v.end()-1);
    copy(v.begin(),v.end(),
        ostream_iterator<int>(cout," "));
       cout << endl;

    // 将所有元素反向输出
    reverse_copy (v.begin(), v.end(), // 源区域
         ostream_iterator<int>(cout," "));
                             // 目标地址
    cout << endl;

    return 0;
}
运行结果:
// 1 2 3 4 5
// 5 4 3 2 1
// 5 2 3 4 1
// 1 4 3 2 5

17、swap——交换两个元素的值

#include <iostream>
#include <assert>
#include <algorithm>
using namespace std; 

int main ()
{
   int x = 1;
   int y = 2;

   cout << "x = " << x << "  y = " << y << endl;
   assert(x == 1 && y == 2);
   swap(x, y);
   assert(x == 2 && y == 1);
   cout << "x = " << x << "  y = " << y << endl;

   return 0;
}
运行结果:
//  x = 1  y = 2
//  x = 2  y = 1

18、swap_ranges——交换两个容器的值

#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert> 

int main ()
{
    vector<int> V1, V2;
    V1.push_back(1);
    V1.push_back(2);
    V2.push_back(3);
    V2.push_back(4);

    assert(V1[0] == 1 && V1[1] == 2 &&
           V2[0] == 3 && V2[1] == 4);
    swap_ranges(V1.begin(), V1.end(), V2.begin());
    assert(V1[0] == 3 && V1[1] == 4 &&
           V2[0] == 1 && V2[1] == 2);
    copy(V1.begin(),V1.end(),
        ostream_iterator<int>(cout," "));
       cout << endl;
    copy(V2.begin(),V2.end(),
        ostream_iterator<int>(cout," "));
       cout << endl;       

    return 0;
}
运行结果:   
// 3 4
// 1 2

19、transform——将指定容器的元素送入指定函数执行后,取其返回值存入指定地址

#include <iostream>
#include <algorithm>
using namespace std; 

double inch_to_cm (double in)
{
    return ( in * 2.54 );
}
//---------------------------------------------------- 

int main ()
{   
    // 一组以英寸为单位的数值
    double inches[] = { 3.5, 6.2, 1.0, 12.87, 4.35 };
    double centi[5];
    double inch_to_cm (double); //定义函数指针
    transform (inches, inches+5, centi, inch_to_cm); 

    //将inches的元素转化为以厘米为单位的数值后存入centi数组

    for (int j=0; j<5; j++)
        cout << centi[j] << ' ';
    cout << endl;

    return 0;
}
运行结果:
// 8.89 15.748 2.54 32.6898 11.049

20、unique——删除指定容器内相邻的重复元素,保证相邻元素值不等

#include <iostream>
#include <algorithm>
#include <vector> 

int main ()
{
    vector V;
    V.push_back(1);
    V.push_back(3);
    V.push_back(3);
    V.push_back(3);
    V.push_back(2);
    V.push_back(2);
    V.push_back(1);

    vector<int>::iterator new_end =
        unique(V.begin(), V.end());
    copy(V.begin(), new_end,
        ostream_iterator<int>(cout, " ")); 

    return 0;
}
运行结果:   
// 1 3 2 1

21、unique_copy——删除指定容器内相邻的重复元素后再拷贝到目标地址,原容器不变

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std; 

int main()
{
    vector<string> v;
    // 从标准输入读入单词存入V容器中
    copy (istream_iterator<string>(cin),
                                      // 源容器头
          istream_iterator<string>(), // 源容器尾
          back_inserter(v));          //目的地址
    copy (v.begin(),v.end(),
           ostream_iterator<string>(cout," "));
    // 为元素排序 
    sort (v.begin(), v.end());
    // 无重复地打印元素
    unique_copy (v.begin(), v.end(), 
      ostream_iterator<string>(cout,"\n"));
}
运行结果:
// Denis Anatoliy Anatoliy Wood Wood Lorens
// Anatoliy
// Denis
// Lorens
// Wood

除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。

本文地址:https://www.cpplive.com/html/316.html

评论 (3)

  • Trevor says:

    话说隔行如隔山嘛,谢谢你的留言。^_^

  • 这里因为你的留言而存在!!!

    You must be logged in to post a comment.