inside a function?
I was working on a hackerrank problem. It allows me to write only inside the provided function.
It uses std namespace, and included vector and iostream.
However I need an unordered map.
So I implemented it using vector inside that function. But now I'm thinking maybe somehow I could include it? With some hacks maybe...
https://en.cppreference.com/w/cpp/preprocessor/include c and cpp inclusion simply copies the content of the file right where your statement is so... go nuts with including
Yeah I know but it didn't work out of box. Compilation errors. I'm guessing it's because of using namespace std
then you should come again with the actual error :)
Yeah that's a topic for another day...
and it prolly worked. you just had as you stated just another error not related to the include statement itself
I do remember, that the error message contained something related to namespace. So tell me this. Assuming that in unordered map header there is a namespace std statement. Is this valid code? using namespace std; ... ... .. void r() { namespace std{...}; }
its valid but you created the namespace std::std inside your r() function which is prolly not what you want
which is actually one of the reasons for the convention having the include statements at the top (which still does not mean that it must always be the case)
Well do I have a choice in this case?
i dont think you can open up the std statement mid block, but if whatever is creating the new namespace is on your included statement, then you could a. remove the using namespace statement (always better) or b. if you are in possesion of the included source, remove the namespace declaration out of it or you could try out declaring the std namespace from the top... like namespace ::std { } although i dont quite know whether it would compile at this point
I can't edit anything outside the function block
> Is there a way to make an include statement inside a function? yes > is there a way to add #include <unordered_map> inside a function? no
the easiest breaking point will be functions defined inside the function
Well I implemented unordered map class inside that function
<unordered_map> probably contains much more than just classes.
Probably they wanted you solve the issue without unordered_map
Quite possible
this is definitely the case
Обсуждают сегодня