首先,你可以使用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