0

My question seems similar to vector of objects vs vector of pointers to objects but reading it hasn't answered my question.

I'll avoid the details for why my program is structured the way it is, because that isn't particularly relevant.

  1. I have three vectors of pointers to 18 streampos vectors of unknown length (depending on input) (lets refer to these streampos vectors collectively as A's(A1,A2...A18), and the three vectors containing A's lets call B's(B1,B2,and B3)
  2. For reasons explained below, I have a vector of pointers to B's, lets refer to this vector as C.

to reiterate, i have a vector(C) containing three pointers each, to vectors(B) containing 18 pointers each, to vectors(A) of streampos.

  • I have a function nuc() which main() calls 120 times.
  • nuc() is recursive and calls itself ~33000 times (2^15).
  • a function check() is called by nuc() once every time nuc() is called (i could therefore move check() into nuc() without changing the functionality much, just making the code a bit messier). Check() is the function which interacts with my A's.

Each time main() calls nuc(), it passes to nuc() a specific B (because there are three B's containing 18 A's each). Every time nuc() calls check(), it passes to it the relevant specific A (e.g. A16). Check() then push_back()s a value into the relevant A.

It's come to my attention that i need to generate an int vector for every streampos vector to carry relevant information, but it seems odd to instantiate literally 108 vectors. I've been thinking of instead instantiating each B as a vector of 18 vectors.

Would i have any issues push_back()ing values into its subvectors? Is there any reason i should choose a vector of vectors over a vector of vector pointers beside the visual silliness of instantiating 108 vectors? what benefits does a vector of vectors have over the former, besides the one i mentioned?

I apologize if the question is pedantic, this is my first C++ program and I looked at about 7 pages on the topic but struggled to find an answer.

here's a pseudocode example:

fstream file1;
vector<streampos> A0_0...A0_18, A1_0...A1_18,...
vector<vector<streampos>*> B0 = {&A0_0...&A0_18},
                           B1 = {&A1_0...&A1_18}...
vector<vector<vector<streampos>*>*> C = {&B0, &B1, &B2}

main()
    string line;
    while(getline(file1,line))
        int B_determiner = line[-1] //suppose 0
        vector<vector<streampos>*> B = C[B_determiner] //suppose B0
        nuc(line, B)


nuc(string line,vector<vector<streampos>*> B) // B = B0
    string new_line = line.do stuff to line...
    A_determiner = new_line[0]
    A = B[A_determiner] //suppose A0, therefore A0_0
    check(new_line, A)

check(string new_line, vector<streampos> A) //A = A0_0
    streampos value = derive a streampos from new_line...
    A.pushback(value)
Kama
  • 39
  • 6
  • That's a lot of prose and very little code. – melpomene Aug 11 '18 at 01:13
  • @melpomene would it be clearer if i wrote it out in code? It's just background information to show what i'm doing, i didn't think code was that relevant to my question as the question isn't meant to work out a bug. – Kama Aug 11 '18 at 01:15
  • If you are not familiar with `c++`, I would suggests *no pointer in code*. – apple apple Aug 11 '18 at 01:20
  • 1
    Well, it's past midnight here and I can't reconstruct your code from a vague description in my head (which I would need to do to figure out what it is you're asking). If the code isn't relevant, why do you spend 9 paragraphs just describing what the code does? – melpomene Aug 11 '18 at 01:20
  • @melpomene posted an example – Kama Aug 11 '18 at 01:52
  • bonus question, I've assumed that because this is the case in python, a function Y() called by function X() cannot modify an object in X() except through "return". I therefore instantiate my As and Bs and Cs globally. Is this also true in C++? – Kama Aug 11 '18 at 01:59
  • @Kama even in python, I believe you can modify a list if you pass it. – apple apple Aug 11 '18 at 02:02
  • @apple apple you are correct, thanks for clearing that up, i really appreciate it. So it works the same way in C++? – Kama Aug 11 '18 at 02:16
  • Seems like a lot of your question should just be "try it and see". If you have specific concerns about specific behaviors, then post those things as individual questions. There's no way we can know if you will have "issues" with one approach or another -- "it's complicated" which makes this question too broad. – xaxxon Aug 11 '18 at 02:20
  • @xaxxon I agree, i just figured as someone who hasn't taken any c++ classes someone with experience could tell me one or two truisms about the vector of vectors vs vector of pointers to vectors issue – Kama Aug 11 '18 at 02:26
  • @Kama that's off topic for stack overflow. This site is for specific programming questions, not generalizations. There are plenty of places you can go to ask general questions like this.. cpplang slack, ##c++-general on irc.freenode.net, /r/cpp_questions, etc.. – xaxxon Aug 11 '18 at 02:28
  • @appleapple "no pointer in code" is bad advice for c++ programmers and often leads to very weird ideas of how to program. Teaching ownership early is very important, not making people scared of important and useful parts of the language. – xaxxon Aug 11 '18 at 02:30
  • @xaxxon I apologize, i didn't know that stackoverflow was that specific, I'll certainly look over the rules. – Kama Aug 11 '18 at 02:36
  • @Kama there's some flexibility, but your question is incredibly broad. – xaxxon Aug 11 '18 at 02:45
  • I'd look at collapsing your fixed size parts into structs, and if you need additional data like a parallel array/vector of ints then put that in there too. What you've described is confusing at best. You also appear to be passing all of the vectors by value in your pseudocode. Presumably that is not the case in your real program. – Retired Ninja Aug 11 '18 at 03:48
  • @xaxxon I (personally) think it's a good advice, for who not familiar with `c++`. I would not teach complicated things like recursive template, boost, *and pointer* to a newbie. There are thing should be taught first. – apple apple Aug 11 '18 at 07:39
  • @Kama you achieve that by [pass by reference](https://en.cppreference.com/book/intro/reference) in `c++`. – apple apple Aug 11 '18 at 07:43

0 Answers0