如何计算 C++ 中向量的索引?

How to count index of vectors in C++?

我有 5 个整数向量 v​​1 到 v5。我想分别计算相同向量的索引。如果下一个向量中出现不同的向量,它会输出先前相同向量的索引并开始对新向量进行计数。任何帮助将不胜感激。

std::vector<int>v1={1, 2, 3};
std::vector<int>v2={1, 2, 3};
std::vector<int>v3={1, 2, 3, 4};
std::vector<int>v4={1, 2, 3, 4};
std::vector<int>v5={1, 2, 3};

输出应该是:向量值=索引向量

{1,2,3} = {0,1};
{1,2,3,4} = {2,3};
{1,2,3} = {4};

您只想搜索向量集合并找到符合您条件的元素在哪些位置?那么可能是这样的:

    std::vector<int> v1={1, 2, 3};
    std::vector<int> v2={1, 2, 3};
    std::vector<int> v3={1, 2, 3, 4};
    std::vector<int> v4={1, 2, 3, 4};
    std::vector<int> v5={1, 2, 3};
    std::vector<std::vector<int>> collection({v1, v2, v3, v4, v5});
    auto found = std::find(collection.begin(), collection.end(), v1);
    while (found != collection.end())
    {
        std::cout << "found at idx = " << std::distance(collection.begin(), found) << std::endl;
        // begin next search after previously found element
        found = std::find(++found, collection.end(), v1);
    }

如果您愿意,也可以使用自定义谓词。