精选文章

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

变序算法实例一

文章分类 : 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。

1、copy_backward——从尾向头逆向拷贝

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
 //用"..........abcdef.........."初始化source
    vector<char> source(10,'.');
    for (int c='a'; c<='f'; c++) {
        source.push_back(c);
    }
    source.insert(source.end(),10,'.');

    // 打印 source 内容
    for (int i = 0; i < source.size(); i++ )
        cout << source[i]; cout << endl;

    // 拷贝所有的字母元素到 'a' 前面的第三位(从头开始顺向拷贝)
    vector<char> c1(source.begin(),source.end());
    copy (c1.begin()+10, c1.begin()+16, // 源区间
          c1.begin()+7);     // 目的地址

    copy (c1.begin(),c1.end(),
         ostream_iterator<char>(cout,"")); //迭代输出
    cout << endl;

    // 拷贝所有的字母元素到 'f' 后面的第三位(从尾开始逆向拷贝)
    vector<char> c2(source.begin(),source.end());
    copy_backward (c2.begin()+10, c2.begin()+16, // 源区间
                   c2.begin()+19); // 目的地址
     copy (c2.begin(),c2.end(),
         ostream_iterator<char>(cout,"")); //迭代输出
    cout << endl;
}
运行结果:
//  ..........abcdef..........
//  .......abcdefdef..........
//  ..........abcabcdef......asdf

2、fill——向指定区间填充指定内容

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

template <class T>
class Print
{
    public:
        void operator () (const T& t)
        {
            cout << t << ' ';
        }
};
//-----------------------------------------------
int main ()
{
    Print<int> DoPrint;
    vector<int> vInt(10);

    fill (vInt.begin(), vInt.begin()+5, 1 );
    fill (vInt.begin() + 5, vInt.end(), 5 );

    for_each (vInt.begin(), vInt.end(), DoPrint);
    cout << "\n\n";

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

3、generate、generate_n——填充容器

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

int main()
{
    list li;

    // 插入五个随机数
    generate_n (back_inserter(li),
   //back_inserter被称作iterator适配器;使元素被插入到作为实参的list的尾部
            5,      // 个数
            rand);  // 新的随机数
    copy(li.begin(),li.end(),
        ostream_iterator<int>(cout," "));

    // 覆盖五个新的随机数
    generate (li.begin(), li.end(), // 目标区域
              rand);              // 新的随机数
       copy(li.begin(),li.end(),
        ostream_iterator<int>(cout," "));
    return 0;
}
运行结果:
// 41 18467 6334 26500 19169
// 15724 11478 29358 26962 24464

4、partition——按照指定条件将容器划分为两部分

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

using namespace std; 

void main()
{
    const int VECTOR_SIZE = 8 ;
    typedef vector IntVector ;
    typedef IntVector::iterator IntVectorIt ;
    IntVector Numbers(VECTOR_SIZE); //创建容器
    IntVectorIt start, end, it;  //创建迭代器
    start = Numbers.begin();
    end = Numbers.end();
    //初始化容器
    Numbers[0] = 6
    Numbers[1] = 20;
    Numbers[2] = 10;
    Numbers[3] = 15;
    Numbers[4] = 12;
    Numbers[5] = 7;
    Numbers[6] = 9;
    Numbers[7] = 10;
    cout << "Before calling partition" << endl ;
    // 打印容器内容
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
    // 给容器分区,小于11的元素全排在大于11的元素前面
  it = partition(start, end, bind2nd(less<int>(),11));
    cout << "After calling partition" << endl ;
    // 打印容器内容
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
    return 0;
}
运行结果:
// Before calling partition
// Numbers { 6 20 10 15 12 7 9 10  }
//
// After calling partition
// Numbers { 6 10 10 9 7 12 15 20  }

5、random_shuffle——从指定区间中随机取值

#include <iostream>
#include <algorithm> 

using namespace std; 

int main()
{
    const int N = 8;
    int A[] = {1, 2, 3, 4, 5, 6, 7, 8};
    random_shuffle(A, A + N);
    copy(A, A + N, ostream_iterator<int>(cout, " "));
}
运行结果:
// 输出结果可能是 7 1 6 3 2 5 4 8,
// 或其他40319种可能(8的阶乘为:40320)

6、remove——从指定区间中删除值为指定值的所有元素,元素数量不变,错位后的剩余元素位保持原值不变

#include <iostream>
#include <list>
#include <algorithm> 

using namespace std; 

int main()
{
    list coll; 

    for (int i=1; i<=6; ++i) {
        coll.push_front(i);
        coll.push_back(i);
    }  
    cout << "pre:  ";
    copy (coll.begin(), coll.end(), 
         ostream_iterator<int>(cout," "));
    cout << endl; 

    // 删除所有值为3的元素 
    remove (coll.begin(), coll.end(), 3);
    cout << "post: ";
    copy (coll.begin(), coll.end(),
         ostream_iterator<int>(cout," ")); 
    cout << endl;

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

7、remove_copy——从指定区间向另一容器拷贝元素,忽略值为指定值的所有元素

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

using namespace std; 

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

    remove_copy(V.begin(), V.end(), 
            ostream_iterator<int>(cout, " "),
            0);

    return 0;
}

运行结果:
// -2 -1 1 2

8、remove_copy_if——从指定区间向另一容器拷贝元素,忽略值不满足指定条件的所有元素

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

using namespace std; 

int main() 
{ 
    vector<int> V1;
    V1.push_back(-2);
    V1.push_back(0);
    V1.push_back(-1);
    V1.push_back(0);
    V1.push_back(1);
    V1.push_back(2); 
    vector<int> V2;
    remove_copy_if(V1.begin(), V1.end(), 
               back_inserter(V2),
               bind2nd(less<int>(), 0));
    copy(V2.begin(),V2.end(),
         ostream_iterator<int>(cout," "));
    cout << endl;

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

9、remove_if——从指定区间中删除值满足指定条件的所有元素

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

int main()  
{ 
    vector<int> V;
    V.push_back(1);
    V.push_back(4);
    V.push_back(2);
    V.push_back(8);
    V.push_back(5);
    V.push_back(7); 

    copy(V.begin(), V.end(), 
         ostream_iterator<int>(cout, " ")); 
    vector<int>::iterator new_end = 
       remove_if(V.begin(), V.end(),  
                  compose1(bind2nd(equal_to<int>(), 0),
                           bind2nd(modulus(), 2)));
                           V.erase(new_end, V.end());
    copy(V.begin(), V.end(), 
        ostream_iterator<int>(cout, " ")); 

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

10、replace——将指定区间内的指定值替全部换为另一值

#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" << endl ;  
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ; 

    // 将所有值为10的元素替换为
    replace(start, end, 10, 35) ; 
    cout << "After calling replace, to replace "
         << "all 10's with 35" << endl ; 
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
}
运行结果:
//  Before calling replace
//  Numbers { 10 20 10 15 12 7 9 10  }
//  After calling replace, to replace all 10's with 35
//  Numbers { 35 20 35 15 12 7 9 35  }

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

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

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

You must be logged in to post a comment.