2

From the below code you can see that the vector array has the same number twice or more than. What I want to do is to find the first two same number's position from the pointer *ptr

 #include<iostream> 
#include<iterator> // for iterators 
#include<vector> // for vectors 
using namespace std; 
int main() 
{ 
    vector<int> ar = { 1,8,2, 2, 2, 5,7,7,7,7,8 }; 

    // Declaring iterator to a vector 
    vector<int>::iterator ptr; 

    // Displaying vector elements using begin() and end() 
    cout << "The vector elements are : "; 
    for (ptr = ar.begin(); ptr < ar.end(); ptr++) 
        cout << *ptr << " "; 
        return 0;     
}

Let's assume I want to print out the first two position and elements of 7 by dereferencing the pointer *ptr. Should I use an if a condition like

int *array = ptr.data(); 
for( int i =0; i < ar.size(); i++) {

if( array[i] - array[i+1]+ ==0)
    cout<<array[i]<<endl;

}

But how would I guarantee that it is not looking for the only first two same elements from *ptr?

UPDATE

Clearing the question:

  1. The reason I always want to know the first and second position of the same element from the dereferencing the pointer is that later I will do some study, and in that study, I will be given some time associated with the first and second position of the same number. The problem, I wanted to ignore the same elements which are still repetitive after the second time is because I want to ignore these element positions in my calculations.
  2. For example, if you print out the code you would find the element: **The vector elements are 1 8 2 2 2 5 7 7 7 7 8 **. In this case, the first two positions of the element 2, is [2], and [3], therefore I would like to ignore the position [4]. Another thing to mention, that I don't care if the value or consequent or not[I mean for example 828, or 888, I would consider both]. For example, the number 8 is in location array[1], and in the [10]. I would also consider this.
zero_field
  • 335
  • 1
  • 11
  • 1
    Could you clarify the question a bit more? – Rietty Oct 20 '19 at 15:18
  • I have updated the question Rietty – zero_field Oct 20 '19 at 15:26
  • Are you interested just in consequent repeating values (like [2 2 2])? From your comments I assume that only consequent repeating values should be reported (so in this example the second occurrence of 2 should not be reported [2 8 2]). What if the same value is repeated more than once in the vector (like in [2 2 8 2 2])? – MartinBG Oct 20 '19 at 15:30
  • That's a good point, Martin. I have updated the question. The number doesn't have to be consequent. The only thing I care if the number is repetitive in the array, and find the first two positions of the same number. – zero_field Oct 20 '19 at 15:38
  • 1
    Unrelated to your issue, but still important: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Max Vollmer Oct 20 '19 at 15:52

2 Answers2

2

Create a map where each value is stored as key, mapped to a list of indices:

std::unordered_map<int, std::vector<size_t>> indexMap;

Loop over your initial values and fill the map:

for (size_t index = 0; index < ar.size(); index++)
{
    indexMap[ar[index]].push_back(index);
}

Now you can loop over your map and work with every value that has 2 or more indices and only use the first 2 indices for whatever you want to do:

for (auto const& [value, indices] : indexMap)
{
    if (indices.size() < 2)
        continue;

    size_t firstIndex = indices[0];
    size_t secondIndex = indices[1];

    // do whatever
}

(If you don't use C++17 or up, use for (auto const& pair : indexMap), where pair.first is value and pair.second is indices.)

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
  • I understand the logic, except what I should use in the initial value? Here: `indexMap[value].push_back(index);` – zero_field Oct 20 '19 at 16:44
1

You could use map or unordered_map to register indexes of each value.

Here's a simple demo of the concept:

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

using namespace std;

int main() {
  vector<int> ar{ 1, 8, 2, 2, 2, 5, 7, 7, 7, 7, 8 };
  map<int, vector<size_t> > occurrences{ };

  for (size_t i = 0; i < ar.size(); ++i) {
    occurrences[ar[i]].push_back(i);
  }

  for (const auto& occurrence:occurrences) {
    cout << occurrence.first << ": ";
    for (auto index: occurrence.second) {
      cout << index << " ";
    }
    cout << endl;
  }

  return 0;
}

Output:

1: 0
2: 2 3 4
5: 5
7: 6 7 8 9
8: 1 10
MartinBG
  • 1,500
  • 13
  • 22