maps

This a general question should we used nullptr with maps?
It depends. The question would be better with an example.
Last edited on
pointers in maps are not uninitialized. So if you write for example

1
2
3
4
5
void foo()
{
  TestClass* p;
  // ...
}


p will not be initialized to nullptr.
Last edited on
let say we make an object

1
2
3
4
std::string getName() const
	{
		return name;
	}

1
2
3
4
5
6
7
8
9
10
11
12
const testClass* testClass2::getName(std::string name) const
testClass *p=nullptr
std::map<std::string, testClass> h;
std::map<std::string, testClass>::const_iterator it;
for(it = h.begin(); it != h.end(); it++)
{
if((*it).secod.getName() ==name)
    {
      p=(*it).second;// why would i get an error here and if i put it  like this '*p' like this i dont get and error
    }
} 
return p;

sorry for the sloppy code just trying to give example.

and you cant do (it !=nullptr) right should you do it this way(it != h.end())
Last edited on
and you cant do (it !=nullptr) right should you do it this way(it != h.end())

No, you can’t. ‘it’ is likely to be an int. To get an ‘invalid’ iterator, it’s common, as you say, to set it to a past-to-end position:
http://en.cppreference.com/w/cpp/concept/Iterator
Iterators are not dereferenceable if they are past-the-end iterators (including pointers past the end of an array) or before-begin iterators. Such iterators may be dereferenceable in a particular implementation, but the library never assumes that they are.


1
2
3
4
testClass *p=nullptr
. . .
        p=(*it).second;// why would i get an error here and if i put it 
                       // like this '*p' like this i dont get and error 

Since ‘p’ is a pointer, you can pass it an address or deference it to set a value into the pointed memory. You can’t pass a value to the pointer itself.
Anyway, at that line, your pointer ‘p’ is still pointing to an unallocated memory area.

sorry for the sloppy code just trying to give example
Ok, but an example of what?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <limits>
#include <map>
#include <string>

// So, do mean you have a class with a getName() method?
class TestClass {
public:
    TestClass() = default;
    TestClass(std::string name_arg) : name { name_arg } {}
    std::string getName() const { return name; }
private:
    std::string name;
};

void waitForEnter();

int main()
{
    TestClass *p = nullptr;
    std::map<std::string, TestClass> h { { "John",  { "Smith" } }, { "Ann", { "Joe" } },
                                         { "Carol", { "Brown"} },  { "Lou", { "Davis" } } };
    std::map<std::string, TestClass>::const_iterator it;
    std::string name = "Joe";
    for(it = h.begin(); it != h.end(); it++)
    {
        if(it->second.getName() == name)
        {
            // You can't do this: p is pointing to a memory area you don't own:
            //p=(*it).second; // why would i get an error here and if i put it 
                            // like this '*p' like this i dont get and error
            p = new TestClass(it->second);
        }
    } 
    std::cout << "p is now " << p->getName() << '\n';

    waitForEnter();
    return 0;   
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Output:
p is now Joe

Press ENTER to continue...

thank you you explain this to me
 
 p = new TestClass(it->second)
questions about two iterator
 
const example* g = (*it).second.getEample((itj*).second.getExample2); //if we get rid of const it an error but if we dont then it not why is that. 
Last edited on
Just wanted to point out that one shouldn't be using new , unless one is writing a library. Especially since in this example a STL container is being used. STL containers already put their data on the heap, and it is bad to use new purely to obtain a pointer. The problem with new is if something throws an exception, the (non existent in this case) delete is never reached, so there is a memory leak.

There is a range based for loop, which is good when one wants to iterate all the items in the container, so one doesn't have to use iterators at all, or even worry about pointers at all.Try to do value or reference semantics, not pointer semantics.

The first version is the safest and ideal : it handles lvalues, rvlaues, const and non-const. The item&& is a forwarding reference, it does perfect forwarding. The auto means automatic type deduction. One can't put const before the auto, put the whole thing in a function, send h as a const reference argument.

1
2
3
4
5
6
// choose a better name than h
for (auto item&&  : h)  { //item is one of the members of the container h
   if ( item.second.getname() == name ) {
      // do something
   }
}


A different version, not as good as the first one:

1
2
3
4
5
6
// choose a better name than h
for (const auto item&  : h)  { item is passed by const reference.
   if ( item.second.getname() == name ) {
      // do something
   }
}


Good Luck!!
then how would you set your object = it->second

and
i have question i made two iterator
this line of code is cause me problem this just an example of it though

1
2
3
4
5
 	

const example* g = (*it).second.getEample((itj*).second.getExample2); //if we get rid of const it an error but if we dont then it not why is that. 

	
then how would you set your object = it->second


In the examples above item is the same as the dereferenced iterator, so it->second is item.second

i have question i made two iterator
this line of code is cause me problem this just an example of it though


Apart from it looking too complicated, and using pointers, I will try to guess. I gather that you are assigning something from a container to a pointer that you invented. That value would have to be const because if it wasn't that would mean the pointer could be used to change that value, which isn't allowed if it is a key in the container.

As I said earlier, try to keep away from the pointers, use references instead.
so you see @enoizat line

 
 p = new TestClass(it->second);


my question was talking about that
like for example

it would be
 
p = item.second;//FOR EXAMPLE 
it would be

p = item.second;//FOR EXAMPLE


I keep saying, get away from the pointers.

You haven't yet shown the details of this "example" class with it's functions. If you explain exactly what you are trying to do, as a real world problem, then we could help you more. As mbozzi said much earlier, it depends on what the situation is.
Topic archived. No new replies allowed.