-2

I stopped while inserting a unique pointer into a std::unordered_map.

when I looking for the answer I got a link : C++ inserting unique_ptr in map.

I tried that but it will not work for me. Here is my sample what I tried till now.

int main()
{
    unordered_map<string,unique_ptr<char[]>> mymap;
    string key = "ac";
    char* ctr = "myvalue";
    unique_ptr<char[]> value = make_unique<char[]>(100);
    memcpy(value.get(), ctr,5);
    mymap.insert_or_assign(key,std::move(value));
    return 0;
}

Getting a compilation issue.

JeJo
  • 30,635
  • 6
  • 49
  • 88
Dhanasekaran Don
  • 294
  • 1
  • 14
  • 2
    Can you post your compile errors? – po.pe Nov 18 '19 at 07:55
  • 2
    your map key is an integer but `key` is a string? Please post a [mre] with the full code and error message – Alan Birtles Nov 18 '19 at 07:55
  • Is there a reason you don't use `std::vector` instead of `std::unique_ptr`? Or, considering your example, `std::string`? – Some programmer dude Nov 18 '19 at 07:55
  • now that your map key is a string the code compiles (bar missing headers): https://godbolt.org/z/K52XnV. please post a [mre] with the full error message – Alan Birtles Nov 18 '19 at 08:04
  • Unrelated: Consider not practising [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – JeJo Nov 18 '19 at 08:09
  • 2
    When asking questions about build errors, always include the *full* and *complete* output in the question itself. And add comments in the [mcve] on the lines where you get the errors. Also please refresh [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Nov 18 '19 at 08:09
  • It's not relevant to your question, but the `memcpy` will truncate the string "myval", and I'm not sure if the array will be default initialized or zero initialized. – Martin Bonner supports Monica Nov 18 '19 at 08:44
  • @AlanBirtles You don't need the `#include ` in your godbolt example. – Martin Bonner supports Monica Nov 18 '19 at 13:00
  • To the OP, if you [edit] your code sample with all the required headers, and include the error message, we'll be happy to re-open this question. If your attempt has resulted in you working out what the problem was, please let us know. – Martin Bonner supports Monica Nov 18 '19 at 13:01
  • 1
    @MartinBonnersupportsMonica yep, I added it because the question said it was using `std::map`, when it didn't compile due to actually using `unordered_map` I forgot to remove it, doesn't change the outcome though – Alan Birtles Nov 18 '19 at 13:26

2 Answers2

3

You have your mymap taking int eger as a key

unordered_map<int,unique_ptr<char[]>>  mymap
//            ^^^

and the key you passing is std::string. Obviously, the compiler can not accept this.

You probably meant to have std::string as key

std::unordered_map<std::string, std::unique_ptr<char[]>> mymap;
//                ^^^^^^^^^^^^

That being said, there is no need of using a smart pointer here, rather simply a

std::unordered_map<std::string, std::string> mymap;
JeJo
  • 30,635
  • 6
  • 49
  • 88
-2

You cannot use unique pointers in STL containers. The unique pointers just transfer the ownership do not share this. Probably an undefined behavior will be occurred when you use unique pointers in STLs containers Copying of containers will leave source containers with invalid data.

getsoubl
  • 808
  • 10
  • 25
  • This is simply not true. You most certainly **can** use unique pointers in STL containers. Implementing unique pointers could be done in C++89 (see std::auto_ptr), the problem was that you couldn't use them in STL containers. C++11 introduced rvalue references, std::move, std::unique_ptr, and made it possible. – Martin Bonner supports Monica Nov 18 '19 at 08:47
  • 3
    Obviously, you cannot *copy* an STL container containing unique pointers, but you can certainly move it. – Martin Bonner supports Monica Nov 18 '19 at 08:48
  • Yes, but inside the containers make copies . Invalidation will be generated – getsoubl Nov 18 '19 at 08:52
  • 1
    Rubbish. There are some methods which copy; they won't compile because std::unique_ptr has no copy constructor or copy assignment operator, but there are always similar methods which move instead. – Martin Bonner supports Monica Nov 18 '19 at 08:56