0
int numSubarraysWithSum(vector<int>& A, int S) {
    unordered_map<int, int> c({{0, 1}});// Line 1
    int psum = 0, res = 0;
    for (int i : A) {
        psum += i;
        res += c[psum - S];
        c[psum]++;
    }
    return res;
}

What does line 1 mean? I'm confused as there are two curly braces.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Initialiser list as mentioned in [given answer](https://stackoverflow.com/a/67732420/1312382). The outer braces define the initialiser list itself, the inner braces are shorthand for constructing `std::pair`s, which are used in the map to store the keys and corresponding values. – Aconcagua May 28 '21 at 03:44
  • 1
    You might see the same construct leaving out the (round) parentheses, which is legal (uniform initialisation – which is done for the inner pairs, too), but I cannot recommend this in general (and especially not in templates), as there are situations where the wrong constructor can be chosen, e. g. `std::vector v{7, 3}` uses the initialiser list constructor, but you might have wanted to select [third variant of](https://en.cppreference.com/w/cpp/container/vector/vector). This is especially critical for classes in development if the initialiser list constructor is added later on. – Aconcagua May 28 '21 at 03:55
  • 1
    So my personal recommendation is to prefer the classic constructor call (round parentheses as you've seen already) over uniform initialisation: `std::vector v(7, 3)` (three sevens) vs. `std::vector v({7, 3})` (one seven, one three) is just so much clearer. – Aconcagua May 28 '21 at 03:58

2 Answers2

2

It's something called an Initilizer list. According to this website:

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon.

Basically it adds elements into your std::map (or in this case your std::unordered_map) right while you're creating it.

More specifically, from @Aconcagua comment above :

The outer braces define the initializer list itself, the inner braces are shorthand for constructing std::pairs, which are used in the std::map to store the keys and corresponding values.

You can see this from this piece of code:

#include <iostream>
#include <map>
#include <unordered_map>
using namespace std;

int main()
{
    map<int, int> c({{0, 1}, {1, 7}});
    for (auto it = c.begin(); it!=c.end(); it++)
    {
        cout << it->first << " " << it->second << "\n";
    }
}

Output:

0 1
1 7

There's also other data types that support this, for example std::vector :

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> test({0, 7, 9, 11, 3});
    for (auto x : test) {cout << x << "\n";}
}

Output:

0
7
9
11
3

There's a lot of question regarding this:

And more info: https://en.cppreference.com/w/cpp/utility/initializer_list

*Cautions : As @Aconcagua mentioned below, using namespace std is not considered a good practice, although it might save some typing time.

  • 1
    Considering that all occurs right in the constructor I'd replace `right after you created it` with `right while creating it`. Even though apparently question author did so, I wouldn't apply `using namespace std` in my own answers for [good reasons](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Apart from, nice answer. – Aconcagua May 28 '21 at 03:41
  • @Aconcagua Thanks for the comment, I'll modified my answer and add the `using namespace std;` cautions. –  May 28 '21 at 03:49
0

It simply just add elements into your map. There are many different ways to do this:

  1. unordered_map<int, int> c{ {0, 1} }; similar to your question

2 unordered_map<int, int> c;
c[0]=1;
assign key and value

3.

 unordered_map<int, int> c;
       c.insert(std:: make_pair (key, vale) );  use make_pair

... You can refer more here map

Văn Ngô
  • 1
  • 4
  • 1
    There's a difference, though, in between 1. and 2./3.: First variant initialises the map directly, allocating sufficient memory right away, while the other two variants first create an empty, default initialised map and only then request the memory. which is (slightly) less efficient. – Aconcagua May 28 '21 at 04:01
  • 1
    I do not recommend leaving out the round parentheses around initaliser lists, see my comments to question. Some might contradict, though, so make up your own mind ;) – Aconcagua May 28 '21 at 04:04