|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
For inserting new elements in map, we can use insert member function.
To know if an element exists or not in a map, we can use count or find member function. Also, we can use the iterator returned by find to modify the mapped value of an existing key. When we use operator[], it may add an element into the map if the key already doesn't exist, which may not be always wanted. Given this, I am unable to understand the reason as to why we have operator[] in map whose functionality can be achieved by other member functions. Kindly clarify. Thanks V.Subramanian |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Feb 7, 2:56 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote: > For inserting new elements in map, we can use insert member function. > > To know if an element exists or not in a map, we can use count or find > member function. > > Also, we can use the iterator returned by find to modify the mapped > value of an existing key. > > When we use operator[], it may add an element into the map if the key > already doesn't exist, which may not be always wanted. > > Given this, I am unable to understand the reason as to why we have > operator[] in map whose functionality can be achieved by other member > functions. > > Kindly clarify. > > Thanks > V.Subramanian You've already got a response on your original post - if that doesn't answer the question, reply and give some information as to how it didn't . Don't just post the same question again, especially on the same day. Rob. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Feb 7, 10:00 am, "[rob desbois]" <rob.desb...@gmail.com> wrote:
> On Feb 7, 2:56 pm, "subramanian10...@yahoo.com, India" > > > > <subramanian10...@yahoo.com> wrote: > > For inserting new elements in map, we can use insert member function. > > > To know if an element exists or not in a map, we can use count or find > > member function. > > > Also, we can use the iterator returned by find to modify the mapped > > value of an existing key. > > > When we use operator[], it may add an element into the map if the key > > already doesn't exist, which may not be always wanted. > > > Given this, I am unable to understand the reason as to why we have > > operator[] in map whose functionality can be achieved by other member > > functions. > > > Kindly clarify. > > > Thanks > > V.Subramanian > > You've already got a response on your original post - if that doesn't > answer the question, reply and give some information as to how it > didn't . > Don't just post the same question again, especially on the same day. > > Rob. If you are referring to the other thread with the subject "using operator[] or insert in map ?", then I would like to say that I got clarified for THAT doubt. If you are referring to some other thread, please give the subject of the thread you are referring to. In this particular thread ie the thread with the subject "need for operator[] in map", I want to know the reason for having operator[] in map. Since the modification of mapped-value for a given key can be done through dereferencing and modifying the iterator returned by find() operation, I - as a beginner in STL, am unable to understand the reason for having operator[] in map. I still think the question I have posted here is different from the other thread with the subject: "using operator[] or insert in map ?" Please provide me an answer for this thread. Thanks V.Subramanian |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Feb 7, 2:56pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote: > For inserting new elements in map, we can use insert member function. > > To know if an element exists or not in a map, we can use count or find > member function. > > Also, we can use the iterator returned by find to modify the mapped > value of an existing key. > > When we use operator[], it may add an element into the map if the key > already doesn't exist, which may not be always wanted. > > Given this, I am unable to understand the reason as to why we have > operator[] in map whose functionality can be achieved by other member > functions. > My guess is: syntactic elegance, convenience, familiarity, symmetry. IOW it's nice to be able to be able to do: foomap["bar"] = "baz"; // then s = foomap["bar"]; You only need to look at code using [] vs the alternatives to see this for yourself. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Feb 7, 3:56 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote: > For inserting new elements in map, we can use insert member function. > To know if an element exists or not in a map, we can use count or find > member function. > Also, we can use the iterator returned by find to modify the mapped > value of an existing key. > When we use operator[], it may add an element into the map if the key > already doesn't exist, which may not be always wanted. > Given this, I am unable to understand the reason as to why we have > operator[] in map whose functionality can be achieved by other member > functions. Convenience. It's a bit awkward, in that the semantics which would be most convenient vary according to what you're using the map for, and in practice, I rarely use []. (But then, most of my maps are const, so I can't.) The real question is, of course, if [] doesn't find the element, what does it do? In the most general case, because of such uncertainty surronding the semantics, one could argue that map shouldn't support an operator[] at all---that its a case of operator overloading abuse. In practice, however, smaller, text based languages like AWK and perl use it effectively, and it seems reasonable to support it in the same way they do---if you are using std::map like you'd use an array in AWK or perl, you use it (and not much else of the interface of std::map); if you are using std::map in some other context, you don't use it. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 2008-02-07 09:56:01 -0500, "subramanian100in@yahoo.com, India"
<subramanian100in@yahoo.com> said: > For inserting new elements in map, we can use insert member function. > > To know if an element exists or not in a map, we can use count or find > member function. > > Also, we can use the iterator returned by find to modify the mapped > value of an existing key. > > When we use operator[], it may add an element into the map if the key > already doesn't exist, which may not be always wanted. > > Given this, I am unable to understand the reason as to why we have > operator[] in map whose functionality can be achieved by other member > functions. > > Kindly clarify. Beside providing some nice syntactic alternatives, it also have some real functional values. For example, you can write template algorithms that assume its input to have random access like that of an array using the operator[] with an indexing set that is not necessarily integer-based. Then, since std::map supports operator[], std::map can participate in such algorithms. An example of such algorithm may be accessing edges of a directed graph based on their terminating vertices as the index. -- // kira |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
James Kanze wrote:
> On Feb 7, 3:56 pm, "subramanian10...@yahoo.com, India" > <subramanian10...@yahoo.com> wrote: >> For inserting new elements in map, we can use insert member function. > >> To know if an element exists or not in a map, we can use count or find >> member function. > >> Also, we can use the iterator returned by find to modify the mapped >> value of an existing key. > >> When we use operator[], it may add an element into the map if the key >> already doesn't exist, which may not be always wanted. > >> Given this, I am unable to understand the reason as to why we have >> operator[] in map whose functionality can be achieved by other member >> functions. > > Convenience. It's a bit awkward, in that the semantics which > would be most convenient vary according to what you're using the > map for, and in practice, I rarely use []. (But then, most of > my maps are const, so I can't.) The real question is, of > course, if [] doesn't find the element, what does it do? Well, the language does not really give you a chance here. Suppose you wanted to not insert a default or dummy value into the map, what would you return? Maybe a proxy like so: #include <map> #include <cassert> #include <iostream> #include <utility> template < typename Key, typename Mapped > struct does_not_work : public std::map< Key, Mapped > // hack! { typedef typename std::map< Key, Mapped >::iterator iterator; class proxy { does_not_work * map_ptr; Key const * key_ptr; iterator where; public: proxy ( Key const & the_key, does_not_work & the_map ) : map_ptr ( &the_map ) , key_ptr ( &the_key ) , where ( map_ptr->find( the_key ) ) {} operator Mapped & ( void ) { assert( where != map_ptr->end() ); return ( where->second ); } operator Mapped const & ( void ) const { assert( where != map_ptr->end() ); return ( where->second ); } Mapped & operator= ( Mapped const & other ) { if ( where == map_ptr->end() ) { where = map_ptr->insert( std::make_pair( *key_ptr, other ) ).first; } else { where->second = other; } return ( where->second ); } Mapped & operator= ( proxy const & other ) { return ( this->operator=( static_cast< Mapped const & >( other ) ) ); } }; proxy operator[] ( Key const & key ) { return ( proxy( key, *this ) ); } }; int main ( void ) { does_not_work< int, int > my_map; my_map[3] = 5; std::cout << my_map[3] << '\n'; std::cout << my_map[0] << '\n'; // triggers assert, could be made throwing } However, that is not really satisfying when it comes to syntactic sugar: my_map[key].some_method(); for instance cannot be done since we cannot overload the dot-operator. Because of that, we cannot have smart-references as a complement to smart pointers and proxy object can fall short of desires and expectations. [snip] Best Kai-Uwe Bux |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
<subramanian100in@yahoo.com> wrote:
> For inserting new elements in map, we can use insert member function. > > To know if an element exists or not in a map, we can use count or find > member function. > > Also, we can use the iterator returned by find to modify the mapped > value of an existing key. > > When we use operator[], it may add an element into the map if the key > already doesn't exist, which may not be always wanted. > > Given this, I am unable to understand the reason as to why we have > operator[] in map whose functionality can be achieved by other member > functions. > > Kindly clarify. For the answer, you will have to ask the standards body, or at least Alex Stepanov (the creator of the STL.) For now, maybe the explanation from SGI's website will do? Note that the definition of operator[] is extremely simple: m[k] is equivalent to (*((m.insert(value_type(k, data_type()))).first)).second. Strictly speaking, this member function is unnecessary: it exists only for convenience. (http://www.sgi.com/tech/stl/Map.html) |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On Feb 8, 2:59 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> James Kanze wrote: > > On Feb 7, 3:56 pm, "subramanian10...@yahoo.com, India" [...] > > Convenience. It's a bit awkward, in that the semantics which > > would be most convenient vary according to what you're using the > > map for, and in practice, I rarely use []. (But then, most of > > my maps are const, so I can't.) The real question is, of > > course, if [] doesn't find the element, what does it do? > Well, the language does not really give you a chance here. A chance for what? There are really three different reasonable semantics for a "get" function on an associative container: 1. return a pointer to the value, returning a null pointer if the value isn't found; 2. throw an exception if the value isn't present, or 3. create a new entry, with a default value (in which case, the "get" function can't be const---in the other two cases, you would probably overload the function on const). The interface of std::map basically implements the first, with the "getter" called find(), not get (or operator[]), except that it returns an iterator, rather than a pointer (and the iterator is to the attribute value pair, not the value). The second option isn't directly available, and operator[] implements the third. I suspect that the reason behind this is that it is easier to simulate the others when you have the first, rather than vice versa---with the second, you need a wrapper with a try block to simulate the others, and with the third, you need some way of testing whether an element is present to simulate the other two. I suspect that the operator[] was added mainly to support the perl/AWK idiom. If you want perl/AWK like associative arrays, you use it (and you pretty much ignore const). Otherwise, you use find() and insert(). -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
![]() |
| Outils de la discussion | |
|
|