Tom Tom - 1 month ago 18
C++ Question

How initialize std::set<std::wstring> *wordDict

I have the following line of code:

std::set<std::wstring> *wordDict = std::unordered_set<std::wstring>();


I got following compile error:

..\Test.cpp:206:71: error: cannot convert 'std::unordered_set<std::__cxx11::basic_string<wchar_t> >' to 'std::set<std::__cxx11::basic_string<wchar_t> >*' in initialization
std::set<std::wstring> *wordDict = std::unordered_set<std::wstring>();


How to fix it ?

Answer

Either

std::set<std::wstring> wordDict;

or

std::unordered_set<std::wstring> wordDict;

depending on what kind of set it it is you actually want.

If you really need dynamic allocation, which is very unlikely because you almost never need to dynamically allocate a standard container:

auto wordDict = new std::set<std::wstring>;

or

auto wordDict = new std::unordered_set<std::wstring>;
Comments