CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2002
    Posts
    756

    Is this possible to write like this?

    Hi, ALL,
    I'm trying to simplify the writing of the for loop on the std::set.

    This is what I'd like to have:

    Code:
    for( std::map<std::string,std::set<std::pair<std::string, double> > >::iterator it = m_score.begin(); it != m_score.end(); it++ )
    {
    	wxString score = (*it).first;
    	if( score == "abc" || score == "def" )
    	{
    			std::set<std::pair<std::string,double> >::reverse_iterator it1 = (*it).second.rbegin();
    			std::set<std::pair<std::string,double> >::reverse_iterator it2 = (*it).second.rend();
    	}
    	else if( score == "ghi" || score == "jkl" )
    	{
    		std::set<std::pair<std::string,double> >::iterator it1 = (*it).second.begin();
    		std::set<std::pair<std::string,double> >::iterator it2 = (*it).second.end();
    	}
    	for( it1; it1 != it2; it1++ )
    	{
    	}
    Obviously this code will fail as it1 and it2 are not defined outside the "if" blocks.
    Is there an easy way to achieve this using C++98 (not C++11)?

    Thank you.

  2. #2
    Join Date
    Jul 2013
    Posts
    576

    Re: Is this possible to write like this?

    Quote Originally Posted by OneEyeMan View Post
    I'm trying to simplify the writing of the for loop on the std::set.
    I don't know if it counts as a simplification but you could unify the forward and the reverse iterators by the way of a bidirectional iterator.

  3. #3
    Join Date
    Aug 2002
    Posts
    756

    Re: Is this possible to write like this?

    Hi, razzle,
    Quote Originally Posted by razzle View Post
    I don't know if it counts as a simplification but you could unify the forward and the reverse iterators by the way of a bidirectional iterator.
    Can I ask you for an example for a bi-directional iterator?
    Never used it before.

    Thank you.

  4. #4
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Is this possible to write like this?

    It will not produce clean code, but instead of the reverse iterator you can check that the container is not empty, and then start iterating from end().
    Code:
    bool backwards = true; // direction of iteration
    if (!container.empty()) {
      // forward
      for (container_type::iterator it = backwards ? --container.end() : container.begin(); backwards || it != container.end(); backwards ? --it : ++it) {
        // do stuff here
        if (backwards && it == container.begin()) break;
      }
    }
    Nobody cares how it works as long as it works

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is this possible to write like this?

    Quote Originally Posted by OneEyeMan View Post
    Hi, ALL,
    I'm trying to simplify the writing of the for loop on the std::set.
    What exactly are you trying to accomplish (on a high-level)? Whenever I see loops/code like this, there seems to be a smarter way of doing what you're trying to do. Usually, an algorithm or set of algorithms does the job, but I would like to know the final goal of what these loops are supposed to accomplish.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Is this possible to write like this?

    Quote Originally Posted by OneEyeMan View Post
    Obviously this code will fail as it1 and it2 are not defined outside the "if" blocks.
    Is there an easy way to achieve this using C++98 (not C++11)?
    You can write your inner loop as a function that is templated on the iterator type. Then call it like this
    Code:
    for( std::map<std::string,std::set<std::pair<std::string, double> > >::iterator it = m_score.begin(); it != m_score.end(); it++ )
    {
    	wxString score = (*it).first;
    	if( score == "abc" || score == "def" )
    	{
    		InnerLoopFunction(it->second.rbegin(), it->second.rend());
    	}
    	else if( score == "ghi" || score == "jkl" )
    	{
    		InnerLoopFunction(it->second.begin(), it->second.end());
    	}
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Aug 2002
    Posts
    756

    Re: Is this possible to write like this?

    Hi, Paul,
    Sorry for the late reply. For some reason I didn't get a notification about you post, even though I came to the forum to check zerver' reply.

    Let me give you and idea of what I'm looking for.

    Lets say I have a following structure on the input:

    [
    { "abc": "Team1" -> 1.234, "Team2" -> 1.234, "Team3" -> 5.6, "Team4" -> 7.0 }
    { "def": "Team1" -> 7.0, "Team2" -> 7.0, "Team3" -> 5.6, "Team4" -> 1.234 }
    ]

    On the output I'd like to have:
    [
    { "abc": "Team1" -> 1.5, "Team2" -> 1.5, "Team3" -> 3, "Team4" -> 4 }
    { "def": "Team1" -> 3.5, "Team2" -> 3,5, "Team3" -> 2, "Team4" -> 1 }
    ]

    The resulting values are 1, 2, 3 and 4, since there are 4 teams in this example. For the "abc" category the resulting values assigned from lowest to highest and it is vice versa for "def" category.
    For "abc": "Team1" and "Team2" have the same value, so to calculate the result it is (1 + 2)/2, i.e. sum of resulting values divided by the count of equal teams.

    As you can see the algorithm for assigning the resulting values is the same, only the loop direction differs.

    Hopefully I express myself clear and you can understand this. If not just ask and I will try to clarify to the best of my ability.

    Thank you.

  8. #8
    Join Date
    Aug 2002
    Posts
    756

    Re: Is this possible to write like this?

    Hi, D Drmmr,
    Thank you for the suggestion.
    I will wait for Paul's response and then see what is the best option.

    I hope you don't mind this. ;-)

    Thank you.

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