### Vector indexing

Hi everyone. Is there any way to give arbitrary indexing to a vector? For example I have a vector a={1,5,6,2,7,0}. Now the indeces are 0,1,2,3,4,5. I want the indeces be like 3,7,5,2,4,1 and whenever I say a[3] it gives me the value 1 or I say a[7] and I receive 5. Is that possible to do something like this?
Last edited on
What happens when you get a[6] in your example?

If I'm understanding you correctly, you want a different type of associative container, like a map (AKA "dictionary" in other languages).

For example std::map<int, int> or std::unordered_map<int, int>.

Then you can have map[7] = 5.
Last edited on
Why would you want random indexing that has no connection with the elements and how by the standard they are stored and accessed?

I'm sure it is possible, but doing it is incredibly stupid, ultimately defeating the purpose of using a container. I sure as hell wouldn't ever consider doing something this dumb.

You do realize that your a[7] indexing of a 6 element container at best creates undefined behavior, right? A possible SEG FAULT if you're lucky. More likely give you whatever crap is at the bogus location.

The closest thing IMO to doing what you want is simply generate a random number between 0 and the number of container elements, minus 1 of course. Use that number to access the container's elements. Now you can arbitrarily pluck a number from the container.
@George P I think we can answer people's question in more respectful way not in the dumb fashion. If you don't know the answer you just simply pass but do not say that is stupid or something when even you don't have any perspective on what purpose or where this is going to be used . I said I want an arbitrary random indexing, What does it matter if I say a[7] or a[10000] (when I assign the 10000 to the third indices in the a vector) ,I am not talking about the size of a that you are telling me it is out of range. It seems you did not understand the question. By the way I wouldn't answer to anyone like this.
Last edited on
@Ganado Thanks for your answer. For a[6], it the real indexing it should give you the value 0, but now if I say a[1] I will get the value stored in a[6] which is 0.
 `map or std::unordered_map`

This is a good Idea.
Last edited on
As Ganado points out using a std::map, or another associative container is better suited to what you want to do. Attempting to use a std::vector is like trying to get a chicken to fly.

It just won't happen easily, or end up without making a huge mess.
 ``12345678910111213141516171819202122232425`` ``````#include #include int main() { std::vector a{1,5,6,2,7,0}; std::vector reference{3,7,5,2,4,1}; // GENERAL for(int i:reference) std::cout << a[i - 1] << ' '; std::cout << '\n'; // LOOKUP int ndx = 5; std::vector::iterator it = std::find(reference.begin(), reference.end(), ndx); int ref = std::distance(reference.begin(), it); std::cout << a[ref] << '\n'; return 0; }``````

 ``` 6 0 7 5 2 1 6 Program ended with exit code: 0```

Shouldn't L7 should be:

 `` `` ``std::vector reference{3,6,5,2,4,1};``

Not according to @OP but can be if you like.
 ``1234567891011121314151617181920212223242526`` ``````#include #include #include using namespace std; template T demap(const vector &a,const vector &r,int idx) { T ret; for (int n =0;n a{1,5,6,2,7,0}; vectors{"a","g","b","w","m","n"}; vector r{3,7,5,2,4,1}; // reference vector. cout<