What is the internal purpose of the loop inside 'for (auto & str: vec)'?

I am new to C ++ and try to learn the concept of a vector. I saw this code on the Internet. My question is, what is the purpose of the inner for loop inside the for loop (auto & str: vec)? Why did the author create a second link (& c) to the first link (& str)?

int main() {
    vector<string> vec;
    for (string word; cin >> word; vec.push_back(word)) {
    }
    for (auto &str : vec) {
        for (auto &c : str) {
            c = toupper(c);
        }
    }
    for (int i = 0; i != vec.size(); ++i) {
        if (i != 0 && i % 8 == 0) cout << endl;
        cout << vec[i] << " ";
    }
    cout << endl;
    return 0;
}
+4
source share
2 answers

It is designed to convert each character of a string strto uppercase.

In other words, it is:

for(auto &c : str) {
   c = toupper(c);
}

is equivalent to:

for(size_t i = 0; i < str.size(); ++i) {
    str[i] = toupper(str[i]);
}
+5
source

. . , c str.

+3

Source: https://habr.com/ru/post/1628072/


All Articles