std::multiset
std::string text = "this is a sample text. this is another sample."; std::istringstream iss(text); std::string word; std::multiset<std::string> wordSet; while (iss >> word) { wordSet.insert(word); } for (const auto& w : wordSet) { std::cout << w << ": " << std::count(wordSet.begin(), wordSet.end(), w) << std::endl; }
std::multiset<int> sensorValues; // 假设不断有传感器值传入 sensorValues.insert(10); sensorValues.insert(15); sensorValues.insert(10); int valueToCount = 10; std::cout << valueToCount << " appears " << std::count(sensorValues.begin(), sensorValues.end(), valueToCount) << " times." << std::endl;
Last updated