CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2015
    Posts
    500

    stl find_if iterator type

    Hello,

    It is strage that that find_if for the vector returns normal iterator, but when i replace it with the set , it return const iterator.

    Basically, i wanted to change the content of the iterator returned by the find_if. I tried with vector, where i could change it. But in case of set i couldnot ( in a sample program from the internet)

    Code:
    // find_if example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::find_if
    #include <vector>       // std::vector
    
    bool IsOdd (int i) {
      return ((i%2)==1);
    }
    
    int main () {
      std::vector<int> myvector;
    
      myvector.push_back(10);
      myvector.push_back(25);
      myvector.push_back(40);
      myvector.push_back(55);
    
      std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
    
      // overwrite the returned value
      *it = 5;
    
      std::cout << "The first odd value is " << *it << '\n';
    
      return 0;
    }
    But in case of set:

    Code:
    #include <iostream>
    
    #include <iostream>     // std::cout
    #include <algorithm>    // std::find_if
    #include <vector>       // std::vector
    #include <set>       // std::vector
    
    bool IsOdd(int i) {
    	return ((i % 2) == 1);
    }
    
    int main() {
    	std::set<int> myset;
    
    	myset.insert(10);
    	myset.insert(25);
    	myset.insert(40);
    	myset.insert(55);
    
    	std::set<int>::iterator it = std::find_if(myset.begin(), myset.end(), IsOdd);
    
    	*it = 9; // 
    
    	std::cout << "The first odd value is " << *it << '\n';
    
    	return 0;
    }
    I wanted to overwrite a value in set, but now i cannot use the find_if . Sorry for this basic discussion, but just thought i will share here . I am forgiven here for my naive questions

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: stl find_if iterator type

    As a set is sorted, you can't change it's key (value) as that would change the ordering. You need to erase (or extract for C++17) and insert.

    Code:
    include <iostream>
    #include <algorithm>
    #include <set>
    
    bool IsOdd(int i) {
    	return ((i % 2) == 1);
    }
    
    int main() {
    	std::set<int> myset;
    
    	myset.insert(10);
    	myset.insert(25);
    	myset.insert(40);
    	myset.insert(55);
    
    	const auto it {std::find_if(myset.begin(), myset.end(), IsOdd)};
    
    	myset.erase(it);
    	//*it = 9;
    	myset.insert(9);
    
    	std::cout << "The first odd value is " << *std::find_if(myset.begin(), myset.end(), IsOdd) << '\n';
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: stl find_if iterator type

    Thankyou so much kaud for your patience to help me .

    Thanks a lot again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured