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.
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?
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <utility>
usingnamespace 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 (constauto& 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.
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
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::vectorappears (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.