由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - How to handle the return type of container.size() in C++
相关主题
问一个coding的skill分享A公司面经
round 1 phone interview today -- Amazon明天bloomberg, 求bless (附前两轮面经)
什么也不管了,给了一个烙印很差的feedbackms面试问了atoi,结果搞了半天我还是搞错了
有好的merge有序数组算法么leetcode combinationsum2 超时
how to handle overflow帮发一个同学面G家onsite的面经
刚phone完MS,好紧张。。。。问一个reverse int的问题
发发我自己的Bloomberg的面经请教一个C++11的问题(设计blocking queue)
C++, empty class has 4 or 6 default function一道面试题
相关话题的讨论汇总
话题: sz话题: size话题: type话题: c++话题: container
进入JobHunting版参与讨论
1 (共1页)
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.

1 (共1页)
进入JobHunting版参与讨论
相关主题
一道面试题how to handle overflow
问个越界的问题刚phone完MS,好紧张。。。。
请教一道Leetcode 题, 多谢发发我自己的Bloomberg的面经
请教leetcode一道题C++, empty class has 4 or 6 default function
问一个coding的skill分享A公司面经
round 1 phone interview today -- Amazon明天bloomberg, 求bless (附前两轮面经)
什么也不管了,给了一个烙印很差的feedbackms面试问了atoi,结果搞了半天我还是搞错了
有好的merge有序数组算法么leetcode combinationsum2 超时
相关话题的讨论汇总
话题: sz话题: size话题: type话题: c++话题: container