0

Is there a possibility to give get_ABCD() parameters to use iterator on another given vector string? Something like get_ABCD(possible_hosts).

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
        
int main() {
        
    void get_ABCD() {
        std::vector<int> rand4w;
        std::vector<std::string> possible_answers = { 
            final_years[rand4w[0]], final_years[rand4w[1]], 
            final_years[rand4w[2]], final_years[rand4w[3]] };
        std::vector<std::string> possible_hosts = { 
            hosts[rand4w[0]], hosts[rand4w[1]], 
            hosts[rand4w[2]], hosts[rand4w[3]] };
        //possibly many other vector strings

        std::vector<std::string>::iterator it;
        it = possible_answers.begin();
        std::random_shuffle(possible_answers.begin(), possible_answers.end()); //shuffle possible answers

        for (it = possible_answers.begin(); it < possible_answers.end(); it++)
            std::cout << "\n" << *it << "\n";
    }
    
    system("pause");
}
Chris
  • 26,361
  • 5
  • 21
  • 42
Alex Pina
  • 11
  • 3
  • 2
    Unrelated: You cannot define a function within another function. You can define a lambda expression, though. – user4581301 Oct 21 '21 at 20:22
  • 1
    "Is there a possibility to give get_ABCD() parameters to use iterator on another given vector string?" - Sure. If you write such a function that takes such iterators as arguments and implement the logic you want, why would that not be possible? – Jesper Juhl Oct 21 '21 at 20:23
  • 4
    The question and the code provided suggests that you may be attempting to learn C++ by guesswork. This is most likely a venture with an ill doom. I strongly recommend learning from high-quality programming texts [such as those listed here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Oct 21 '21 at 20:30
  • @JesperJuhl Well, I need help with the logic... I guess I'm I having a chronic misunderstanding of parameters.... Of course, I can reuse this function and change the string and make all the modifications, but won't work if there are 20 separate vector strings... – Alex Pina Oct 21 '21 at 20:32
  • @AlexPina why can't you make your function take a pair of iterators describing the range you want to operate on for arbitrary containers? – Jesper Juhl Oct 21 '21 at 20:37
  • @AlexPina *"I need help with the logic"* -- this comes after defining your parameters. If you need help knowing if C++ supports function parameters, then focus on that question before asking about the logic. If you are ready to ask for help with the logic, add a code snippet showing the function with its parameters and explain the difficulty you had converting the original function to use these parameters. – JaMiT Oct 22 '21 at 03:38

0 Answers0