1

Why map :: find need my parameter to not be const?

 3 years ago
source link: https://www.codesd.com/item/why-map-find-need-my-parameter-to-not-be-const.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Why map :: find need my parameter to not be const?

advertisements
std::map<Item*, item_quantity_t> _items;

bool Inventory::hasItem(Item const& item) {

    return (_items.find(&item) != _items.end() );

};

This code won't work, but changing the input type of "hasItem" to Item & item will work... can someone explain why to me? I've seen that std::find takes a const reference, so passing it a const object should be ok, at least it's what I understand


You've defined that the key type for your map is a pointer to a non-const Item, Item*. The find method expects a (const reference to a) value of the key type, so it requires a pointer to non-const.

When you expand the templates, the parameter type of find is Item* const&.

You can either change your hasItem to take a non-const reference Item&, or you can change your key type to be a pointer to a const Item Item const*.

Tags stl

Related Articles

in grep, why does he need to escape [while not for]

there is one line in file data, as [overall], .... when using grep to obtain this line, I find this can be done only by grep "\[overall]" data just out of curiosity, Why does it need to escape [ while not for ]. EDIT: this is tested on ubuntu 12

Why does Munmap need a parameter as a parameter?

I was wondering, why should the size of mapped memory being one parameter passed in, since there couldn't more more than one mapping starting from same address (could they ?), why won't linux kernel record both start address, length together, but let

Why is ClassManifest needed with Array but not List?

Define the following code: import scala.collection.JavaConversions._ val iter:java.util.Iterator[Any] = Array[Any](1, 2, 3).iterator def func(a:Any):String = a.toString def test[T:ClassManifest](iter:java.util.Iterator[Any], func:Any=>T):Array[T] = i

Why have the variad model parameter packs not been extended?

#include<tuple> #include<iostream> using namespace std; class A { public: int v; A(int a) : v(a){} operator int (){return v;} }; class B { public: int v; B(int a) : v(a + 1) {} operator int (){return v;} }; class C { public: int v; C(int a) :

why `tasklist | find & ldquo; cmd.exe & rdquo; `not working on win7 but xp and vista?

Hi I run tasklist | find "cmd.exe" but has different result on the following OS: winXP: get the cmd.exe process success Vista: get the cmd.exe process success win7: find: cmd.exe: No such file or directory, but tasklist | grep "cmd.exe"

Why Map a loadFactor and List does not have it?

When one creates a Map or a List in Java, they both have the same default initial capacity of 10. Their capacity grows as they get new elements. However, the List only grows when the 11th element is added and the Map grows already when the 8th elemen

Why the window.location.href parameter does not remove script execution

The following code changes the location to www.bing.com regardless of wether redirect is 1 or any other number. If redirect is 1, it logs "is redirecting" and then redirects to www.bing.com. My best guess is that when href is set a change-event

Why do I need a reinterpret_cast to convert Fred ** const to void ** const?

I have a const pointer to a pointer to a Fred and I don't understand why a static_cast isn't sufficient. typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const ppVoid = static_cast<void ** const>(ppFred); Please

C ++: Any reason why the function returning the pointer should not be const?

I wanted to know whether there's any reason a function returning a pointer shouldn't be const. I'm working on some code that seems to be const-correct in most places, but for some reason doesn't declare functions returning pointers as const. For exam

Why do I need a finalizer if my class implements IDisposable?

What about below disposable pattern? using System; public class MyClass : IDisposable { public void Dispose() // Implement IDisposable { //just do the cleanup GC.SuppressFinalize(this); } } Sorry for confustion. I meant to say, if there are no un-man

Why do we need to delete the allocated memory in the C ++ assignment operator?

This question already has an answer here: Why delete is needed in the definition of the copy-assignment operator? 4 answers Why do we need the delete statement? const MyString& operator=(const MyString& rhs) { if (this != &rhs) { delete[] this

Why do we need to specify the size of the column when passing a 2D array as a parameter?

Why can't my parameter be void example(int Array[][]){ /*statements*/} Why do I need to specify the column size of the array? Say for example, 3 void example(int Array[][3]){/*statements*/} My professor said its mandatory, but I was coding before sch

Why do we need a generic here? Is the protocol not enough?

I found the following example on the web about using generics together with protocols, however I don't understand why do we need generics at all, when all we need is to use a protocol. We define a protocol: protocol Healthy { mutating func setAlive(s

Why do we need & ldquo; map & rdquo; be part of MapReduce?

The programming model MapReduce consists of 2 procedures, map and reduce. Why do we need the map part, when we can simply do the mapping inside reduce function. Consider the following pseudocode: result = my_list.map(my_mapper).reduce(my_reducer); Th

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK