reading info form a vector pair and array

I have an vector declared as vector<pair<pair<int, int>, int> >adj[];

is it possible to iterate through the vector and search for a value stored in the first int, and return a value stored in the second int, if the first int is found. There are multiple of the same values stored in the first int.


vector declaration
 
   vector<pair<pair<int, int>, int> >adj[];
Just to be clear: What you currently have isn't just a vector<...>, it's an array of vector<...>, with a size you have not specified.
Is that intended?

When you say "search for a value stored in the first int", are you referring to the int in the outer pair, or the first int in the inner pair?
PS: It would probably be more readable if you turned this into vector of some sort of named struct, in my opinion.
What is this construct supposed to actually represent?
Last edited on
i was told not to declare it, I did have it declared and was told it was wrong?

the int in the first pair. the very first one that is declared
I don't know what your instructor said or meant, but I would imagine they meant you should remove the "[]" from the declaration.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <utility>

using namespace std;

using my_pair = pair< pair<int, int>, int >; // for my sanity

int search(const vector<my_pair>& vec, int search_value)
{
    // iterate through vector
    for (const auto& element : vec)
    {
        // Search for a value stored in the first int (of the inner pair)
        int value = element.first.first;

        // if the first int is found
        if (search_value == value)
        {
            // return a value stored in the second int
            return element.first.second;   
        }
    }
    
    // TODO: What happens if element is not found?
    std::cout << "ERROR: Element not found!\n";
    return -1;
}

int main()
{
    vector<my_pair> adj;
    
    adj.push_back( my_pair { {41, 42}, 43} );
    adj.push_back( my_pair { {104, 105}, 106} );
    
    int resultA = search(adj, 41);
    std::cout << "resultA = " << resultA << '\n';
    
    int resultB = search(adj, 104);
    std::cout << "resultB = " << resultB << '\n';
}

resultA = 42
resultB = 105


Note that I never used the pair.second.second, since I don't know what you want to do with it.
Last edited on
I more than likely never explained myself well enough with the whole vector array declaration and thats why i was told to remove it. Thank you for the code, I will try that
The code did work, but i do need it to be an array of vectors, how would I edit it to work with that?
To properly use a C-style array you have to declare its size before use. Either on the stack or on the heap with new[]. You declared an array on the stack with ZERO elements, and it can't be resized during run-time without a lot of work-arounds.

Either you are misunderstanding the requirements of the assignment, or the person who created the assignment is a sadist. A C-style array with a type of std::vector with a type of nested std::pairs is not something a sane person would require.

A C-style array of std::vector appears (to me) to attempt to create a 2 dimensional container, very badly.

Maybe posting what the assignment requirements are so we can better decipher what needs to be done to go forward.
If we go back to your original declaration,you could do something like:
1
2
3
4
5
6
7
8
9
10
    vector<my_pair> adj[] = {
        vector<my_pair> { my_pair { {41, 42}, 43},
                          my_pair { {100, 101}, 102} },
                          
        vector<my_pair> { my_pair { {51, 52}, 53},
                          my_pair { {200, 300}, 222} },
                          my_pair { {300, 300}, 322} }           
    };
    
    search(adj, 2, 41);


The "search" function I made would be changed to have a outline like:
1
2
3
4
int search(const vector<my_pair> arr[], int num_elements, int search_value)
{
    // return (...);
}


And now you just need to loop num_elements times over arr, and then within each loop of that, loop over each vector.
Last edited on
Topic archived. No new replies allowed.