Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这个向量
vector <string> data data = ["this is", "data that", "is in", "this is", "vector", "vector", "vector"]
如何获得一个向量(或二维数组)来删除重复项,而是为每个第 i 个条目计数?
IE
results = [("this is", 2), ("data that", 1), ("is in", 1), ("vector", 3)]
直接的解决方案是将唯一值及其计数累积到地图中:
std::map<std::string, std::size_t> results; std::for_each(begin(data), end(data), [&](std::string const& s) { ++results[s]; });
这具有线性 (n lg n) 时间复杂度,但因为它必须复制每个不同的字符串值,所以它可能相当昂贵。您还可以对列表进行就地排序,然后计算每个值的数量,如果您有一个移动感知的std::string.
std::string