Technupe
Technupe

Reputation: 4961

I want to write a function that iterates through a list?

I want to write a function that iterates through a list, but takes two elements if they equal a target.

The logic could be to have two loops; first iterate through list and see if any element + the first = target, then second, then third etc..

int i;
int max = list.size;
for(i=0; i<=max; i++)
{
    if()
}

in c++

    int i;
int max = resistors.size();
int curr;
    for(curr = 0; curr<=resistors.size(); curr++)
    {
   for(i=1; i<=max; i++)
       {
        if((resistors[curr]+resistors[i])==target){
            ComposeSerial(resistors[curr], resistors[i]);

        }
       }

}

i believe one thing is missing to make sure that it goes through again?

Upvotes: 0

Views: 164

Answers (2)

Daniel Gallagher
Daniel Gallagher

Reputation: 7105

It might help if you thought about it slightly differently. You said "first iterate through list and see if any element + the first = target". What if you phrased it as "Iterate through the list and see if the current element + any other element in the list = target". All I've done is consolidated your placement of "the first, then second, then third etc..." into the "current element".

Your original phrasing suggests only one loop, with some magic to get the first element, then the second, etc. This reversed phrasing suggests two loops: one to get each element, then an inner loop to test the current element with every other element in the list. Hopefully that way of thinking will clear up any confusion.

Edit:

Since this didn't clear up all the confusion, I'm going to add some pseudocode:

for all items in list
    for all items in list
        if element from outer loop and element from inner loop meet your condition
             return some value

One obvious optimization would be for the inner loop to start after the element from the outer loop, like so:

for all items in list
    for all items in list after the element from the outer loop
        ...

Depending on the problem, you may want to include the outer element at the start of the inner loop:

for all items in list
    for all items in list starting at the element from the outer loop

To write the actual code, grab your textbook, or the nearest C++ reference, or go to cplusplus.com, look up the documentation for the specific container type you're using, and determine the best way to iterate through your elements from there.

Edit 2 - Response to Edited Question

Thanks for the updated question. The only issues I see here are, as @leetNightshade pointed out in the comment, the inner loop should start at curr + 1 (or curr, if you want to allow the test for an element with itself, which you probably don't), and your loops should terminate when curr < resistors.size(), and i < max, respectively. Otherwise, you will include one element past the end of your list in your loop, which will have nasty consequences.

Upvotes: 1

leetNightshade
leetNightshade

Reputation: 2831

Okay, this question was a confusing/weird one, but I've got something that's still a work in progress for you. It's a bit weird, but bear with me.

template<typename V>
V Add(const V v1, const V v2)
{
    return v1 + v2;
}

template<class T,typename V>
bool FindTwoEqualTarget(const T& list, const V target, V& elem1, V& elem2, V (*Process)(const V v1, const V v2) = Add)
{
    for(T::const_iterator iter = list.begin(); iter != list.end(); )
    {
        T::const_iterator iter2 = iter;
        iter = iter2++;
        for( ; iter2 != list.end(); ++iter2)
        {
            if(target == Process((*iter),(*iter2)))
            {
                elem1 = (*iter);
                elem2 = (*iter2);
                return true;
            }
        }
    }

    return false;
}

You can use it like this:

list<int> iList = list<int>();
iList.push_back(5);
iList.push_back(6);
iList.push_back(1);
iList.push_back(4);
iList.push_back(3);
iList.push_back(9);

int val1;
int val2;
FindTwoEqualTarget(iList, 11, val1, val2);

I know you want the function to return a list, I'm playing around with some things now to easily set this up for you, but you can work off of this for now I think.

Upvotes: 0

Related Questions