-3

suppose you have three global vectors V1, V2, and V3.

suppose you have a function which performs a set of actions, such as VN[3]++, on one of the above vectors determined by an int value.

in python i would do something like:

global:
    v1 = [1,2,3]
    v2 = [1,2,3]
    v3 = [1,2,3]
    lists = [v1, v2, v3]

def function (determiner):
    list = lists[determiner]
    list[1] += 1...

I think in theory i could just have separate ifs for each possible value of determiner, but it seems like bad code to repeat a length of code multiple times. (1) What is the correct way to approach this problem? I assume I would use pointers, but I've just learned about them today and i've been struggling to get my code to work. Here's a sample of the code i've been trying.

   vector <int> counts0;
   vector <int> counts1;

   void editor(int determiner){
        if (determiner == 1) {
            vector<int> & count_l = counts1;
        }
        else if (determiner = 2) {
            vector<int> & count_l = counts2;
        }
        count_l[5]++;
    }
Kama
  • 39
  • 6
  • 1
    "suppose you have three global vectors V1, V2, and V3" - why would you want to have such things? –  Aug 06 '18 at 00:35
  • 1
    Can you reduce this question to a single question about the primary thing you are concerned about? It's confusing having to answer 4 questions in 1. – sashang Aug 06 '18 at 00:36
  • @sashang removed questions 2 & 3 – Kama Aug 06 '18 at 00:37
  • I'm not sure why pointers would make this any better, your use of references here is fine. Given the code, I'm not sure if this design is ideal but suppose you did have global vectors and you needed to edit parts of them, there's nothing really wrong with what you're doing. I don't see how pointers would help. Note your `else if` have errors, it changes the value of `determiner` and also there's no actual `if` – Tas Aug 06 '18 at 00:47
  • @NeilButterworth It's a bioinformatics problem that would take a while to explain clearly. suppose you have a bag of Yen coins, GBP Pence, and USD coins, you have a list of coin names for each nationality (meaning three lists), and a list of counts for each coin name associated with each national name list. You remove a coin, if it's on a list, you update the associated count list; if it isn't you add it to the appropriate name list, and add a position with a value of 1 to the associated count list. This is an analogy, in reality each national list has thousands of lines. – Kama Aug 06 '18 at 00:50
  • 1
    None of that explains why they should be global. –  Aug 06 '18 at 00:51
  • 2
    Would it make sense to create a matrix or list / vector of vectors? vector < vector > is valid C++. Either that, or a vector of structs? ex: https://stackoverflow.com/questions/12375591/vector-of-vectors-to-create-matrix – Dave S Aug 06 '18 at 00:52
  • @Tas yeah sorry i deleted the first "if" because i got frustrated not being able to indent in the text box, ill fix that. also !!!! thank you for pointing out my error! with '=' I already made that mistake earlier today! thanks for pointing that out – Kama Aug 06 '18 at 00:54
  • @NeilButterworth I made them global because they have to be called by multiple functions, knowing that, can you recommend a better approach? – Kama Aug 06 '18 at 00:57
  • 2
    Pass them by reference to the functions that need to use them. –  Aug 06 '18 at 00:58
  • @Kama slightly off-topic, you can avoid those errors by raising your warning level, having warnings as errors, or using `const` where applicable. Here, since `determiner` doesn't change it should be marked `const` then the compiler would error about your use. – Tas Aug 06 '18 at 00:59
  • @NeilButterworth to be fair this thread is asking how to pass by reference, if i'm understanding correctly. – Kama Aug 06 '18 at 01:04
  • @tas actually determiner is determined by a different function " check()", which, according to the above example, reads the name list for the name of a coin you took out, and returns its "index", (so if you pull out a nickle, the function finds that "nickle" is at index 3 in "dollars.txt" and returns 3 to determiner, so that the nickle count in the dollar vector is is ++'ed. – Kama Aug 06 '18 at 01:09
  • @DaveS I can see where you're coming from, the reason i didn't take that approach is because i don't know how to move to the vector and then one of the vector's positions, e.g. vectors[2][1]++ – Kama Aug 06 '18 at 01:11
  • @tas sorry i meant to update but i timed out, i can read up on const, but I was trying to say that determiner changes after a couple times editor() is called: at my current configuration, every 1120 calls. – Kama Aug 06 '18 at 01:16

1 Answers1

1

There are two ways to achieve this, depending on what you expect. If lists should reference the vectors, use pointers (as you said, just remember to dereference before indexing)

std::vector<int> a, b, c;
std::vector<std::vector<int>*> lists = {&a, &b, &c};

void editor(int determiner)
{
    (*lists[determiner])[5]++;
}

If you want a copy of all vectors in list, don't use pointers (this can be expensive when you modify lists a lot, only use this approach with const data).

std::vector<std::vector<int>> lists = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};

void editor(int determiner)
{
    lists[determiner][5]++;
}
dan
  • 303
  • 1
  • 3