How to traverse all elements in a map in STL. Please give an instance

Source: Internet
Author: User
Tags knowledge base
Http://topic.csdn.net/t/20020309/18/565440.html

How to traverse all elements in a map in STL. Please give the example owner vlmf) 18:09:15 in C/C ++/C Language Question

How to traverse all elements in a map in STL. Please give an instance
Question count: 50. Reply times: 5top

Hhdsq (ROGUE baby) on the first floor)Back to 18:19:32 score 40

It has not been verified since it was migrated from the essential C ++:

# Include <map>
# Include <string>
# Include <iostream>

Int main ()
{
Map <string, int> words;
Map <string, int >:: iterator it = words. Begin ();
For (; it! = Words. End (); ++ it)
Cout <"key:" <it-> first
<"Value:" <it-> second <end1;
Return 0;
} Top

2 floor hhdsq (ROGUE baby)The score is 0 at 18:21:07.

Of course, the map of words must first contain elements .. Top

Fangrk on the third floor (add fuel, buddy !)The score is 0 at 20:36:46.

Http://www.csdn.net/expert/topic/552/552836.xml? Temp =. 4185602.
"I want to write a function whose parameter is a string and the return type is Char. The function is to return the most frequently occurring character in the string. Please give the implementation code, thank you very much !!!!"

// BCC 5
# Include <map>
# Include <iostream>
Using namespace STD;
Char maxcount (const char *);

Void main ()
{Char buff [200];
Cout <"Please input string:" <Endl;
Cin> Buff;
Cout <"The Max count char
In buff is: "<maxcount (buff) <Endl;
}
Char maxcount (const char * string)
{Map <char, int> c_map;
Const char * P = string;
While (* P ){
C_map [* p] ++;
P ++;
}
Int max = 0;
Map <char, int >:: iterator it = c_map.begin ();
Char find = it-> first;
For (; it! = C_map.end (); It ++ ){
If (max <it-> second ){
Max = it-> second;
Find = it-> first;
}
}
Return find;
}
Top

Zheng_can (nothrow) on the 4th floor)Back to 20:48:28 score 10

Map <>:: iteratortop

Fangrk on the fifth floor (add fuel, buddy !)The score is 0 at 15:42:03.

Why not give the score?

====================================

Ask a question about STL map Traversal Reward score: 20-solution time: I read a lot of articles that traverse map, and there are a lot of code in it, but it also makes me very confused. I want to know which of the following is traversal?

For (iterator it = begin (); it! = End (); ++ it)

Or

For (iterator it = begin (); it! = End (); It ++)

What is the difference between the two ??Question added:Aruo0228:

I know. I want to know if the results of the two traversal methods are the same? Why are there two types of traversal on the Internet?

For two methods:

For (iterator it = begin (); it! = End (); ++ it)

{

Return it-> second;

}

For (iterator it = begin (); it! = End (); It ++)

{

Return it-> second;

}

Are the results returned each time the same ?? Questioner: lemonbox-trial level 1

Best Answer

Two
The iterator traversal times are the same, but the efficiency in STL is different. Before ++ -- returns the reference, and then ++ -- returns a temporary object, because iterator is a class template
In the form of it ++, a useless temporary object is returned, and it ++ is a function overload, so the compiler cannot optimize it. Therefore, each element is traversed, you created and destroyed a useless temporary
Object.

If you don't believe it, you can check the standard library of C ++ and the textbooks that comply with the standard C ++. In addition to the special needs and built-in types, basically, ++ it is used for element traversal, both in source code and textbooks.

The overload of user-defined type operators should be similar to the behavior of built-in operators, and post-auto-increment/Subtraction is often referenced as a copy of the actual row.

For example, it is usually in this form:

Class foo

{

Public:

Foo & operator ++ () {return ++ bar ;}

Foo operator ++ (INT)

{

Foo TMP = * This; // create a temporary object★

+ * This; // auto-increment before calling

Return TMP; // return a temporary object★

}

PRIVATE:

Int bar;

}

Above mark★The two steps of the number are sometimes redundant. For example, using iterator in STL to traverse the container causes unnecessary program efficiency loss.

This is also the details that are frequently ignored by programmers who have transplanted from C to C ++, so they are called the bad habits of programming from C to C ++.

More effective tive C ++

Item 6: distinguish between prefix and Postfix forms of increment and decrement operators.

We will give a detailed explanation of the efficiency problems caused by the Front/back auto-increment/subtraction operators in C ++ and the heavy loads caused by C ++. The following is a part of content:

If you're the kind who worries about efficiency, you probably broke
Into a sweat when you first saw the postfix increment function. That
Function has to create a temporary object for its return value (see
Item 19), and the Implementation abve also creates an explicit
Temporary object (oldvalue) that has to be constructed and destructed.
The prefix increment function has no such temporaries. This leads
The possibly startling conclusion that, for efficiency reasons alone,
Clients of upint shocould prefer prefix increment to postfix increment
Unless they really need the behavior of postfix increment. Let us be
Explicit about this.

When dealing with user-defined types, prefix increment shocould be
Used whenever possible, because it's inherently more efficient. (note this sentence)

Let us make one more observation about the prefix and Postfix
Increment operators. Reset t for their return values, they do the same
Thing: They increment a value. That is, they're supposed to do the same
Thing. How can you be sure the behavior of postfix increment is
Consistent with that of prefix increment? What guarantee do you have
That their implementations won't diverge over time, possibly as
Result of different programmers maintaining and enhancing them? Unless
You 've followed the design principle embodied by the code above, you
Have no such guarantee. That principle is that postfix increment and
Decrement shoshould be implemented in terms of their prefix counterparts.
You then need only maintain the prefix versions, because the Postfix
Versions will automatically behave in a consistent fashion.Respondent:

Cloudification of the world-level 9 8-28 20: 35 http://zhidao.baidu.com/question/34261203.htmlyour location: homepage> technical channel> body

Binder class for Processing Map traversal: depair

[Add this page to favorites] [print]

[It168 Knowledge Base] Because the STL library widely uses iterator, functor, and binder, you can write an adapter class that can be integrated into the STL library to improve the versatility of the Code. Ratio
For example, the input functor type in STL algorithm must be returntype & func (type &
ELEM), while the map stores both the key and value, sometimes the returntype & func (keytype & Key,
Valuetype & Value) callback, you can write such a functor Template <typename operation>

Class depair

: Public unary_function <

Pair <

Typename operation: Inherit, typename operation: second_argument_type>, typename operatoin: result_type >{public: typedef pair <typename operation: Inherit, typename operation: Role> argument_type; typedef typename operation: result_type; depair (const operation & func): op (func) {}; result_type operator () (const argument_type & Arg) {return OP (Arg. first, Arg. second);} result_type operator () (const argument_type & Arg) const {return OP (Arg. first, Arg. second) ;}protected: Operation op ;};

 With depair, you can use the following code: Void traverse (const Int & Key, const string & value){ } Map <int, string> mymap; STD: for_each (mymap. Begin (), mymap. End (), depair <STD: ptr_fun (traverse)> ()); And depair can be used with other binder, suchVoid check (const Int & Key, const string & Value, int check_value){ } Map <int, string> mymap; STD: for_each (mymap. Begin (), mymap. End (), depair <boost: BIND (check, _ 3,100)> ()); In the previous line of code, first use boost: bind to bind the third parameter of check to 100, that is, the check_value passed to the check method is fixed to 100, then bind to depair to process map traversal.
View Original

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.