Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Лямбда-выражения в STL


Автор: shilovec5377 11.6.2015, 17:45
дравствуйте. Помогите пожалуйста с программой. Использование STL
Задания
1. Написать пример работы с заданной коллекцией: продемонстрировать добавление, удаление элементов из коллекции, проход по коллекции элементов (через итератор и через for), модификацию элементов.
2. Написать пример использования заданных контейнеров с заданными алгоритмами без функтора.
3. Написать пример использования заданных контейнеров с заданными алгоритмами с функтором. Использовать разные виды функторов, обязательно попробовать лямбда-выражения.

В примерах с лямбда-выражениями попробуйте, пожалуйста, реализовать передачу локальных аргументов вне лямбда-выражения в лямбда-выражение через добавление их в [].

Контейнер - vector
Алгоритмы без функтора - unique, find
Алгоритмы c функтором - none_of, sort, copy_if 
Код

#include <iostream>
#include <iomanip>
#include <exception>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>

int main()
{
    std::vector<std::string> Strings; // с циклом for
    std::vector <std::string>::iterator _it;
    Strings.push_back("String one");
    Strings.push_back("String two");
    Strings.push_back("String three");

    std::cout << "\nShow our vector: ";

    if (!Strings.empty())
    { 
        std::copy(_it = Strings.begin(), _it = Strings.end(),std:: ostream_iterator<std::string>(std::cout," ") );
    }

    std::cout << "\nNumbers of elemets in vector: " << Strings.size();

    std:: vector<std::string> Strings_2 = Strings;

    std::cout << "\nCopy Strings in Strings_2: ";

    std::copy( _it = Strings_2.begin(), _it = Strings_2.end(), std::ostream_iterator<std::string>(std::cout," ") );

    std:: cout << "\nFirst element of Strings_2: " << Strings_2.front().c_str() << std::endl;
    std:: cout << "\nLast element of Strings_2: " << Strings_2.back().c_str() << std::endl;

    Strings_2.push_back("ADD ELEMENT IN BACK");

    std::cout << "Show our new vector: "<<std::endl;
    std::copy( _it = Strings_2.begin(), _it = Strings_2.end(),std:: ostream_iterator<std::string>(std::cout," ") );

    std::cout<<"\nReverse: "<<std::endl;

    for(auto rit=Strings_2.rbegin();rit<Strings_2.rend();++rit)
        std::cout << *(*rit).c_str() << " ";

    _it = Strings_2.begin();
    Strings_2.insert (_it,"***");
    _it = Strings_2.begin() + 3;
    Strings_2.insert(_it,"////");
    Strings_2.insert(_it+1,"----");

    std::cout<<std::endl;
    std::cout << "Using an iterator to insert into an vector of strings: "<<std::endl;

    std::cout << "Show our new vector: " << std::endl;
    std::copy( _it = Strings_2.begin(), _it = Strings_2.end(), std::ostream_iterator<std::string>(std::cout," ") );
    std::cout << std::endl;

    //функтор
    struct cmp    
    {
        bool operator ()(std::string a, std::string b) const
        {
            return a.length() < b.length(); 
        }
    } cmp;
    //сортировка
    std::sort(Strings_2.begin(), Strings_2.end(), cmp);

    std::cout << "\n\nShow our new vector after sort: " << std::endl;
    
    std::copy( _it = Strings_2.begin(), _it = Strings_2.end(), std::ostream_iterator<std::string>(std::cout," ") );
    std::cout << std::endl;
    //... вывод
    system("PAUSE");
    return 0;
}




Автор: Cheloveck 11.6.2015, 17:57
Что не получается?

user posted image

Автор: shilovec5377 11.6.2015, 18:14
нету Алгоритма без функтора - unique, find.
подскажите куда его можно прописать и как?

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)