Subscripting vectors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main()
{
    size_t n, m;
    cin >> n >> m;
    vector<string> marks(n);
    for (size_t i = 0; i < n; ++i)
    {
        cin >> marks[i];
    }
    vector<bool> successful(n, false);
    for (size_t subject = 0; subject < m; ++subject)
    {
        char best = '0';
        for (size_t i = 0; i < n; ++i)
        {
            if (marks[i][subject] > best)
            {
                best = marks[i][subject];
            }
        }
        for (size_t i = 0; i < n; ++i)
        {
            if (marks[i][subject] == best)
            {
                successful[i] = true;
            }
        }
    }
    cout << count(successful.begin(), successful.end(), true);
    return 0;
}


This was a code I found, and it's really confusing me. How can you subscript a vector just like a 2d array without it being a 2d vector(vector of a vector). and how can you input to a vector without push back ? these 2, mainly confuse me
if (marks[i][subject] cin >> marks[i];
Thanks in advance!
its std::vector<std::string> and operator [] is also overloaded for std::string and that's what the second [] refers to
oi , so you mean [0][1] refers to first string the vector and 2nd element in that string?
yes,
1
2
3
4
5
6
7
8
9
# include <iostream>
# include <string>
# include <vector>

int main()
{
   std::vector<std::string> myVec {"hello", "world"};
   std::cout << myVec[0][3] << " " << myVec[1][2] << "\n";
}
Topic archived. No new replies allowed.