A sudden question just come up with

How are they different in C++? [] and {}

And how do you call them?

I just wonder why we write as str {"hello"}, not str["hello"] or just str {hello}, str[hello].

thanks.
None of your post makes any sense. There is never a case where you could interchange square brackets with curly braces - they are very different. Curly braces are always used for scopes and uniform initialization.

Could you post some example code of what you are talking about?
Last edited on
{} can be used with many things in C++

Variable declarations:
 
int variableInt {5};

Class declarations:
1
2
3
4
5
6
7
class Example
{
public:
    //
private:
    //
};

[] can be used for accessing a certain element of an array or vector
1
2
3
4
5
//declare array
int numbers[5] = {5, 6, 7, 8, 9};

//display first element
cout << numbers[0] << endl;
Last edited on
To LB / I was about to ask you to determine when or which situation I am supposed to use {} or []. and now " There is never a case where you could interchange square brackets with curly braces" makes me think they are very obviously different in C++ and there's no rule to determine which to use over all the C++ , but just have to memorize it.
Thanks.

To Lbkulinski / Thank you so I have to memorize which braces should be used for each situation, and there are no rules to determine it over all the C++?
Knowing what braces to use when is something you learn quickly - they have very specific uses. I don't think 'memorize' is the right word though. Just read and write lots of code and you will get used to it.
Yeah Thanks a lot !
There are "rules", very strict rules. The C++ Standard is the rules. Simply reading the Tutorials on this site very carefully should give a good start.
Topic archived. No new replies allowed.