여기 code가 있습니다. 배열에서 모든 Apple 요소를 제거하는 방법을 찾는 데 문제가 있습니다. 배열에 있는 사과를 셀 수 있습니다. 누군가가 도울 수 있기를 바랍니다 ...
string items[10]= { "Apple", "Oranges", "Pears", "Apple", "bananas", "Apple", "Cucumbers", "Apple", "Lemons", "Apple" };
//Counts the total amount of apples
int n= sizeof(items) /sizeof(items[0]);
cout << "Number of times Apple appears : "
<< count(items, items + n, "Apple");
//remove the element Apple from array
if (string items[].contains("Apple"))
{
items[].remove("Apple");
}
배열의 요소를 "제거"할 수는 없습니다.
Ted Lyngmo2022-01-16 22:51:24관련 없음: sizeof(items) /sizeof(items[0]); int가 아닌 size_t를 반환하므로 auto n= std:size(items);`가 괜찮을 것입니다.
Ted Lyngmo2022-01-16 22:53:40
제거된 항목으로 무엇을 하시겠습니까? 제거한 후에도 여전히 10개 요소의 배열이 있기 때문입니다. std::vector를 사용하는 경우 std::erase_if를 사용하여 벡터를 상기시키고 축소할 수 있습니다.
rturrado2022-01-16 22:50:24