首先,您可以使用operator==
比较std::string
类型的字符串:
std::string a = "asd";
std::string b = "asd";
if(a == b)
{
//do something
}
其次,如果 10000 是数组的大小,则代码中有错误:
array3[m]=array3[m+1];
在这一行中,您正在访问m+1
st 元素,m
最多可达 10000。这意味着您最终将尝试访问第 10001 个元素,并摆脱数组键。
最后,您的方法是错误的,这种方法不会让您删除所有重复的字符串。一个更好的(但不是最好的)方法是这样的(伪代码):
std::string array[];//initial array
std::string result[];//the array without duplicate elements
int resultSize = 0;//The number of unique elements.
bool isUnique = false;//A flag to indicate if the current element is unique.
for( int i = 0; i < array.size; i++ )
{
isUnique = true;//we assume that the element is unique
for( int j = 0; j < result.size; j++ )
{
if( array[i] == result[j] )
{
/*if the result array already contains such an element, it is, obviously,
not unique, and we have no interest in it.*/
isUnique = false;
break;
}
}
//Now, if the isUnique flag is true, which means we didn't find a match in the result array,
//we add the current element into the result array, and increase the count by one.
if( isUnique == true )
{
result[resultSize] = array[i];
resultSize++;
}
}