Example #1
0
int main(int argc, char** argv) {
    std::vector<std::string> words;
    std::string s;
    while (std::cin >> s)
        words.push_back(s);
    for (auto &w : words)
        std::cout << w << " ";
    std::cout << std::endl;
    biggies(words, atoi(argv[1]));

    return 0;
}
Example #2
0
/* generic algorithms
 * algorithm never execute container operations
 */
static int test_generic_algorithm()
{
    std::string s("hello world");

    //find
    auto ret = std::find(s.cbegin(), s.cend(), 'k');
    if (ret != s.cend())
        cout << *ret << endl;

    int a[] = { 13, 2, 72, 13, 38};
    auto ret2 = std::find(std::begin(a), std::end(a), 72);
#if _cplusplus > 201103L
    if (ret2 != std::cend(a))
        cout << *ret2 << endl;
#endif
    //accumulate
    auto sum = std::accumulate(std::begin(a), std::end(a), 0);
    cout << sum << endl;

    //equal
    std::array<int, sizeof(a) / sizeof(*a)> a2{13, 2};
    auto ret4 = std::equal(std::begin(a), std::end(a), a2.begin());
    cout << ret4 << endl;

    //fill
    std::fill(a2.begin(), a2.end(), 8);

    //fill_n
    vector<int> vec;
    auto it = std::back_inserter(vec);
    *it = 32;

    std::fill_n(std::back_inserter(vec), 10, 0);
    cout << "vec size is " << vec.size() << endl;

    //copy
    vec.resize(0);
    std::copy(std::begin(a), std::end(a), back_inserter(vec));

    //replace
    std::replace(vec.begin(), vec.end(), 13, 28);

    //replace_copy
    vec.resize(0);
    std::replace_copy(std::begin(a), std::end(a), back_inserter(vec), 13, 28);

    string input_text{"the quick red fox jumps over the slow red turtle"};
    vector<string> story_text;
    std::stringstream input_st(input_text);
    string si;
    while (input_st >> si) {
        story_text.push_back(si);
    }

    //sort
    std::sort(story_text.begin(), story_text.end());

    //unique
    auto ret5 = std::unique(story_text.begin(), story_text.end());
    story_text.erase(ret5, story_text.end());

    print(story_text);

    vector<string> story_text_2;
    std::copy(story_text.begin(), story_text.end(), std::back_inserter(story_text_2));
    std::stable_sort(story_text.begin(), story_text.end(), isShorter);
    std::sort(story_text_2.begin(), story_text_2.end(), isShorter);
    print(story_text);
    print(story_text_2);

    auto ret6 = biggies(story_text_2, 4);
    std::for_each(ret6, story_text_2.cend(), [](const string &s)->void {cout << s << " "; });
    cout << endl;

    //lambda
    {
        int i1 = 1;
        int ii = 10;
        {
            /*const*/ int i2 = 2;
            int ii = 20;
            auto f = [=, &i2] ()  mutable {cout << ++ii <<  " " << ++i2 << endl; };
            f();
        }
    }

    //bind
    test_bind();

    //insert iterator
    {
        std::list<int> lst = { 1, 2, 3, 4 };
        std::deque<int> lst2, lst3;
        std::copy(lst.begin(), lst.end(), std::front_inserter(lst2));

        *std::front_inserter(lst2) = 5;
        *std::back_inserter(lst2) = 0;
        *std::inserter(lst2, lst2.begin()+ (lst2.end()-lst2.begin())/2) = 10;

        for_each(lst2.begin(), lst2.end(), [](int v) {cout << v << " "; });
        cout << endl;
    }

    //istream/ostream iterator
    {
        std::ostream_iterator<int> oit(cout, " ");
        oit = 1;
        *oit = 2;
        cout << endl;
        std::vector<int> vec= { 1, 2, 3, 4, 5 };
        std::copy(vec.begin(), vec.end(), oit);
        cout << endl;

        //	std::istream_iterator<int> iit(cin);
        //	std::istream_iterator<int> eof;

//		iit++;	//在这里要输入两次, 因为++ --是读取下一个值,在读取下一个值之前,先要读取得到当前值
//      int n = -1;
//      n = *iit++;
//      cout << n << endl;
//
//      assert(++iit == eof);

//      cout << "begin loop" << endl;
//      vec.clear();
//      int vec_size;
//      while (iit != eof) {
//          vec.push_back(*iit++);
//          vec_size = vec.size();
//      }
//      std::copy(vec.begin(), vec.end(), oit);
//      cout << endl;
    }

    //reverse iterator
    {
        string line{"first, middle, last"};
        auto it = std::find(line.cbegin(), line.cend(), ',');
        cout << string(line.cbegin(), it) << endl;

        auto rit = std::find(line.crbegin(), line.crend(), ',');
        cout << string(rit.base(), line.cend()) << endl;	//base成员函数得到正向的迭代器
    }

    return 0;
}
Example #3
0
int main()
{
std::vector<std::string> s {"the","quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
biggies(s,4);
}