C++ Trie using std::map and std::unique_ptr I'm learning C++ and whilst trying to write in modern C++, I've made an attempt to rewrite a trie implementation written in C found here: http://www.geeksforgeeks.org/trie-insert-and-search/

It uses arrays to hold the branches in each node and because it's C it was done using malloc() and no freeing of memory was done in the example.

Is this an efficient approach of representing a trie in C++11? what other ways can I store children in trie nodes?

#include &lt;iostream&gt;
#include &lt;map&gt;
#include &lt;algorithm&gt;
#include &lt;memory&gt;

class Trie {
    struct Node;
    typedef std::unique_ptr&lt;Node&gt; spNode;
    struct Node {
        std::map&lt;char, spNode&gt; children;
        bool isLeaf;
        Node() : isLeaf{false} {}
    };

    spNode root;
public:
    Trie();
    void insert(const std::string&amp; str);
    bool search(const std::string&amp; str);
};

Trie::Trie():root{nullptr}{}

void Trie::insert(const std::string&amp; str) {
    if (root == nullptr) {
        std::unique_ptr&lt;Node&gt; node(new Node());
        root = std::move(node);
    }

    Node *temp = root.get();
    for (const char&amp; c : str) {
        if (temp-&gt;children.find(c) == temp-&gt;children.end()) {//if char not in map
            std::unique_ptr&lt;Node&gt; node(new Node());
            temp-&gt;children[c] = std::move(node);
        }

        temp = temp-&gt;children[c].get();
    }

    temp-&gt;isLeaf = true;
}

bool Trie::search(const std::string &amp;str) {
    if (root == nullptr) return false;

    Node *temp = root.get();
    for (const char&amp; c : str) {
        if (temp-&gt;children.find(c) == temp-&gt;children.end())
            return false;
        temp = temp-&gt;children[c].get();
    }
    return (temp-&gt;isLeaf);
}

int main (void) {
    std::string words[] = { "Hello", "hi", "hey", "howdy", "ho"};
    Trie test;

    for (const auto&amp; str : words) {
        test.insert(str);
    }


   if (test.search("Hello"))
       std::cout &lt;&lt; " 'Hello' is found in the trie\n";
    else
       std::cout &lt;&lt;" 'Hello' is not found in the trie\n";

   if (test.search("yo"))
       std::cout &lt;&lt; " 'yo' is found in the trie\n";
    else
       std::cout &lt;&lt; " 'yo' is not found in the trie\n";


}
I don't see the need for unique_ptr for the root node.

spNode root;


I would just make this a node.

Node root;


The use of std::map is fine. But it does have O(log(n)) lookup. If you switch back to an array its O(1). Its a time for space thing. Pick the one you want.

I don't like the two line creation of nodes.

        std::unique_ptr&lt;Node&gt; node(new Node());
        temp-&gt;children[c] = std::move(node);


Just use reset:

        temp-&gt;children[c].reset(new Node());


Or if you have C++14 use std::make_unique()

        temp-&gt;children[c] = std::make_unique&lt;Node&gt;();


Personally search() does not seem quite the correct verb.

bool search(const std::string&amp; str);


What is wrong with find()?