Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get value from a vector by value from another vector?

I have two vectors:

one contains numbers and names of things;

second collects numbers that have already been showed to the user;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m trying to make a history list of all objects that have been shown.

Here is my code:

class palettArchive{
private:
    std::vector<std::pair<int,std::string>> paletts;
    int palletsCounter;
    std::vector<int> choosen;
public:
    //...
    void history(){
        auto  printHist = [](int& i){
            int tmp = i;
            std::pair<int,std::string> tempPair = paletts[tmp];
            std::cout << tempPair.first << " " << tempPair.second;
            return 0;
        };
        std::for_each(choosen.begin(), choosen.end(), printHist);
    }
};

There is an error:

error: 'this' cannot be implicitly captured in this context
         std::pair<int,std::string> tempPair = paletts[tmp];

I can’t make a third vector with the list that is created already. I need to make it by calling a function and printing at the time.

>Solution :

The lambda must capture this to be able to access member variables:

auto  printHist = [this](int& i){ ... };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading