-
January 13th, 2014, 02:22 AM
#1
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.
-
January 13th, 2014, 03:11 AM
#2
Re: Is this possible to write like this?
Originally Posted by OneEyeMan
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.
-
January 13th, 2014, 03:43 AM
#3
Re: Is this possible to write like this?
Hi, razzle,
Originally Posted by razzle
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.
-
January 13th, 2014, 04:29 AM
#4
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
-
January 13th, 2014, 05:19 AM
#5
Re: Is this possible to write like this?
Originally Posted by OneEyeMan
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
-
January 13th, 2014, 09:03 AM
#6
Re: Is this possible to write like this?
Originally Posted by OneEyeMan
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
-
January 17th, 2014, 04:49 AM
#7
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.
-
January 17th, 2014, 04:52 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|