Question: Is there any STL/Boost function in C++ that allows me to find the indices of all unique elements in a vector?

I have seen many solutons to find unique elements, but I need their index.

```
  vector<int> v = { 1,1,1, 2,2,2,2, 3, 3, ,4,5,5,5,5,5,5 };// already sorted

```

Either I need first index of unique elemnt

```
vector<int> unique_index={0,3,7,9,10};

```

or I need last index of unique elements

```
vector<int> unique_index={2,6,8,9,15};

```

Answer: 
Similar to David's answer of using `std::set`, you could also use `std::map` with its member function `try_emplace(key, value)`:

```
#include <iostream>
#include <vector>
#include <map>

int main (void) {

    std::vector<int> v = { 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5 };
    std::map<int, int> m;

    for (size_t i = 0; i < v.size(); i++)
    {
        // `i` is only entered if the `m[v[i]]` isn't filled yet.
        m.try_emplace(v[i], i);
    }

    for (auto [valueFromV, indexFromV] : m) 
    {
        std::cout << indexFromV << '\n';
    }
}

```
you can change this code and using int instead of a string.

```
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>

std::map<std::string, int> get_unique_indices(const std::vector<std::string>& products) {
    std::vector<std::string> tempProducts(products);
    std::map<std::string, int> result;
    std::sort(std::begin(tempProducts), std::end(tempProducts));
    for (auto it1 = std::begin(tempProducts), it2 = it1; it1 != std::end(tempProducts); it1 = it2) {
        int duplication = 0;
        for (; it2 != std::end(tempProducts) && (*it2 == *it1); ++it2) {
            duplication++;
        }
        if (duplication == 1) {
            result.insert({ *it1, std::find(std::begin(products), std::end(products), *it1) - std::begin(products) });
        }
    }
    return result;
}

int main()
{
    using namespace std;
    vector<string> products = { "apple", "orange", "lemon", "apple", "kivi", "orange", "kivi", "melon"};
    auto result = get_unique_indices(products);
    for (const auto& [key, value] : result) {
        std::cout << key << " " << value << std::endl;
    }
    return 0;
}

```

At first, I create another vector to save the original data. (Cause the position of the item will change in the sort method).

then I use two iterators to find duplicate items and count them. the result will be saved in a map instance.
this code must compile with c++17 onwards.