首先,你可以使用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.这意味着您最终将尝试访问10001st元素并脱离阵列绑定。
最后,你的方法是错误的,这种方式不会让你删除所有重复的字符串。 一个更好的(但不是最好的)的方式来做到这一点是这样(伪):
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++;
}
}
你是什么意思字符串数组中的额外元素? array3是什么类型的?你也应该指出哪种语言。 – Rndm 2012-07-16 06:52:23
字符串数组中的每个元素都有多个实例。我的意思是不止一个相同的字符串。我只需要其中一个 – bahadirtr 2012-07-16 06:53:44
你能解释“每个元素的多于一个实例吗?如果数组有”AABCDD“,所以你想要”ABCD“?? – Swanand 2012-07-16 06:58:07