I have a char vector arr and a vector arrnode which has its elements as node. Following is the code:
struct node { int min; int sum; }; vector<char> arr; char c; for(int j = 0; j < n; j++) { cin >> c; arr.push_back(c); } vector<node> arrnode; for(int j = 0; j < n; j++) { /* if(arr[j]=='(') arrnode.push_back({1,1}); else arrnode.push_back({-1,-1});*/ arrnode.push_back( ( ( arr[j]=='(' ) ? {1,1} : {-1,-1} ) ); } This code gives the following error for the line where ternary operator is used.
prog.cpp:68:49: error: expected ‘:’ before ‘{’ token prog.cpp:68:49: error: expected primary-expression before ‘{’ token However, the if-else part(which is commented) makes the code work fine. What am I missing? Thanks in advance and sorry for the shabby title of the question :)
arrnode.push_back(((arr[j]=='(')?({1,1}):({-1,-1})));Hope this helparr[j] == '(') ? (node){1,1} : (node){-1,-1}, but I'm not sure it's standard (reminds me to C99 compound literals, and C99 isn't C++...){}are not in themselves an expression, if I understand correctly. I think section8.5.4of the standard is what you need, it covers where list initialization can take place and the ternary expression is not listed.