ratkke
ratkke

Reputation: 469

One line input. Ints and not ints

i am new at c++, i've coded before it just on python, so, this is a new world for me and sorry if this question is obvious. Today, i've tried to write my first codeforces on c++, and i get stucked. I have one line of ints, like

10, 10, 10, 13, 1341, 134, 134, 184431

with the lengts m. In this particular example, m=8. How can I read them and save to my array/vector? And there is an another question, if i have non-standart input, for example, n and m symbols in( *, #) like

####
****
#*#*

There is n=3, m=4. If i want to represent # like 1 and * like 0, and save it in may vector of vectors, how can i do that?

Thank you in advance

Upvotes: 0

Views: 84

Answers (1)

user2033018
user2033018

Reputation:

Regarding your first question:

int m;
std::cin >> m;

std::vector<int> v(m);
for (auto i = 0; i < m; ++i)
    std::cin >> v[i];

And the second:

int n, m;
std::cin >> n >> m;

std::vector<std::vector<int>> matrix(n, std::vector<int>(m));
std::string line;

for (auto i = 0; i < n; ++i)
{
    std::cin >> line;
    for (auto j = 0; j < m; ++j)
        if (line[j] == '#')
            matrix[i][j] = 1;
        else
            matrix[i][j] = 0;
}

Upvotes: 2

Related Questions