Eteration of C++ vector

Asked 1 years ago, Updated 1 years ago, 66 views

How do I write vector literacy? Source code 1 is running normally, but source code 2 shows warning.

C++ I just learned it In the past, unsigned was something that was told to be very careful unsigned It's a little scary to use

for (unsigned i=0; i < polygon.size(); i++) {
    sum += polygon[i];
}
for (int i=0; i < polygon.size(); i++) {
    sum += polygon[i];
}

comparison between signed and unsigned integer expressions.

c++ stl

2022-09-22 22:30

1 Answers

See link

It's the same as etherating from the back. You can change the starting position and the decreasing iterator to increasing.

* * * * * * * when the index is unsigned / signed int / size t the and container of size type the on. The reason is in iterating from down on paper.

for(std::vector<T>::iterator it = v.begin(); it != v.end(); ++it) {
    /* /* std::cout << *it; ... */
}
for(std::vector<int>::size_type i = 0; i != v.size(); i++) {
    /* /* std::cout << someVector[i]; ... */
}
for(element_type* it = a; it != (a + (sizeof a / sizeof *a)); it++) {
    /* /* std::cout << *it; ... */
}
for(std::size_t i = 0; i != (sizeof a / sizeof *a); i++) {
    /* /* std::cout << a[i]; ... */
}


2022-09-22 22:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.