-1
ho检查动态数组的某些索引是否为空?动态数组的索引是否为空?
string *array = new string [size];
...some code
while(array[index] != NULL) //this gives error
i++;
感谢..
ho检查动态数组的某些索引是否为空?动态数组的索引是否为空?
string *array = new string [size];
...some code
while(array[index] != NULL) //this gives error
i++;
感谢..
如果string
在你的代码只是一个std::string
,使用string::empty。
对于默认构造的string
,empty()
将始终返回true。如果您想在某个时候清除字符串,请使用string::clear。在致电clear()
后,empty()
将始终返回true。
所以,你的代码应该是这样的:
string *array = new string [size];
...some code
while(!array[index].empty())
i++;
注意,你不检查,如果人指数为空“ - array
是一个字符串数组,所以所有“插槽”包含有效的字符串对象。您只能检查这些对象中有些是否为空 - 这就是这段代码正在做的事情。
谢谢...工作 – 2015-04-04 19:01:59
一个数组没有“空”元素。所以你不能检查一个元素是否为空。 – juanchopanza 2015-04-04 15:09:23
顺便说一下,有一个算法'std :: find_if_not'来查找不满足谓词的第一个元素。 – chris 2015-04-04 15:13:01