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<int, int> or std::unordered_map<int, int>

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> a{1,5,6,2,7,0};
    std::vector<int> 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<int>::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<int> reference{3,6,5,2,4,1};

Not according to @OP but can be if you like.
Thanks for your answers.

A trivial method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>
#include<string>
#include<iostream>
using namespace std;

template <typename T>
T demap(const vector<T> &a,const vector<int> &r,int idx)
{
    T ret;
    for (int n =0;n<r.size();n++)
    { if (r[n]==idx)  return a[n];}
     cout<<idx<< ": invalid_argument"<<endl;
    return ret;                          
  }
  
  int main()
  {
  	vector<int> a{1,5,6,2,7,0};
  	vector<string>s{"a","g","b","w","m","n"};
    vector<int> r{3,7,5,2,4,1}; // reference vector.
    cout<<demap(a,r,5)<<endl;
    cout<<demap(s,r,2)<<endl;
    cout<<"press return to exit . . ."<<endl;
    cin.get();
  }
 
Topic archived. No new replies allowed.