p****n 发帖数: 69 | 1 The return type of container.size() is size_t. If used as such, however, it
may complicate codes sometimes. See the following example:
size_t sz = container.size();
for(size_t i = 0; i < sz -1; i++){
..... // do something
}
The empty container has to be handled in a different way, perhaps tested
before entering the for loop. Now, if we insist using size_t and empty
container is not a problem, we could still encounter the following problem:
size_t sz = contianer.size();
while( sz > 0 ){
...... // the expression may contain --sz or sz -= 2;
}
Again, the fact that size_t is an unsigned type is a pain in the ass.
I noticed many sample codes posted here suggesting the following usage
int sz = container.size();
Associated is the possibility of overflowing, since size_t is implementation
dependent and is likely larger than int. Of course we could use long long,
but it looks weird.
I wonder what is the best way to handle this problem? | p*****p 发帖数: 379 | 2 你有iterator
你有for each
it
【在 p****n 的大作中提到】 : The return type of container.size() is size_t. If used as such, however, it : may complicate codes sometimes. See the following example: : size_t sz = container.size(); : for(size_t i = 0; i < sz -1; i++){ : ..... // do something : } : The empty container has to be handled in a different way, perhaps tested : before entering the for loop. Now, if we insist using size_t and empty : container is not a problem, we could still encounter the following problem: : size_t sz = contianer.size();
| d**********x 发帖数: 4083 | 3 so i would say you did not use your brain at all...
can you use "i + 1 < sz" instead?
it
implementation
,
【在 p****n 的大作中提到】 : The return type of container.size() is size_t. If used as such, however, it : may complicate codes sometimes. See the following example: : size_t sz = container.size(); : for(size_t i = 0; i < sz -1; i++){ : ..... // do something : } : The empty container has to be handled in a different way, perhaps tested : before entering the for loop. Now, if we insist using size_t and empty : container is not a problem, we could still encounter the following problem: : size_t sz = contianer.size();
| S**I 发帖数: 15689 | 4 To iterate elements in a STL container, you should use iterator instead of
index. To check whether a container is empty or not, you should use
container.empty() instead of container.size() > 0.
it
【在 p****n 的大作中提到】 : The return type of container.size() is size_t. If used as such, however, it : may complicate codes sometimes. See the following example: : size_t sz = container.size(); : for(size_t i = 0; i < sz -1; i++){ : ..... // do something : } : The empty container has to be handled in a different way, perhaps tested : before entering the for loop. Now, if we insist using size_t and empty : container is not a problem, we could still encounter the following problem: : size_t sz = contianer.size();
| d**********x 发帖数: 4083 | 5 seriously, who will use iterator for vector<> ?
【在 S**I 的大作中提到】 : To iterate elements in a STL container, you should use iterator instead of : index. To check whether a container is empty or not, you should use : container.empty() instead of container.size() > 0. : : it
| p****n 发帖数: 69 | 6 the 2nd example, plz
【在 d**********x 的大作中提到】 : so i would say you did not use your brain at all... : can you use "i + 1 < sz" instead? : : it : implementation : ,
| S**I 发帖数: 15689 | 7 First, LZ didn't say the container is a vector type; second, if using
iterator, the silly mistake of using i < sz - 1 won't happen.
【在 d**********x 的大作中提到】 : seriously, who will use iterator for vector<> ?
| d**********x 发帖数: 4083 | 8 use an index i instead of directly changing sz
then it's the same as the first one.
【在 p****n 的大作中提到】 : the 2nd example, plz
| p****n 发帖数: 69 | 9 what if I have something like vector used, e.g. to generate all
permutations from a given set (given by a vector). In this case, index
is easier to use.
【在 p*****p 的大作中提到】 : 你有iterator : 你有for each : : it
| p****n 发帖数: 69 | 10 thx
i think to make minimum modification, i could write something like
while(true){
...//before --sz, add
if(sz<=1) break;
...//before sz -= 2, add
if(sz<=2) break;
....
}
【在 d**********x 的大作中提到】 : use an index i instead of directly changing sz : then it's the same as the first one.
|
|