Похожие чаты

Hey guys. It's there a way to make an include statement

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...

19 ответов

7 просмотров

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

Ishkhan- Автор вопроса
Sunbro
https://en.cppreference.com/w/cpp/preprocessor/inc...

Yeah I know but it didn't work out of box. Compilation errors. I'm guessing it's because of using namespace std

Ishkhan
Yeah I know but it didn't work out of box. Compila...

then you should come again with the actual error :)

Ishkhan- Автор вопроса
Ishkhan
Yeah I know but it didn't work out of box. Compila...

and it prolly worked. you just had as you stated just another error not related to the include statement itself

Ishkhan- Автор вопроса
Sunbro
and it prolly worked. you just had as you stated j...

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{...}; }

Ishkhan
I do remember, that the error message contained so...

its valid but you created the namespace std::std inside your r() function which is prolly not what you want

Sunbro
its valid but you created the namespace std::std i...

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)

Ishkhan- Автор вопроса
Ishkhan
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

Ishkhan- Автор вопроса
Sunbro
i dont think you can open up the std statement mid...

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

Ishkhan
Why not?

the easiest breaking point will be functions defined inside the function

Ishkhan- Автор вопроса
Chandradeep Dey
the easiest breaking point will be functions defin...

Well I implemented unordered map class inside that function

Ishkhan
Well I implemented unordered map class inside that...

<unordered_map> probably contains much more than just classes.

Ishkhan
Well I implemented unordered map class inside that...

Probably they wanted you solve the issue without unordered_map

Ishkhan- Автор вопроса

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта