bool true or false

am reading this piece of code and am having a lil tough time with line 14..
I have if not dup... which should mean if dup is different from the value of dup at line 6??? am I right?.. so it means if dup is changing elsewhere in the code the initial declaration of dup holds true or what? am not getting it..
someone help me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template <typename T>
std::vector<T> CreateUniqueVector(const T* firstItem, const T* lastItem)
{
std::vector<T> vReturn;

bool dup = false;

	for (const T* curItem = firstItem; curItem != lastItem; ++curItem,dup = false ) 
        {
		for (const T* chkItem = firstItem; chkItem != lastItem && !dup; ++chkItem)
			{if ((chkItem != curItem) && (*chkItem == *curItem)) 
			dup = true;

		if (!dup)
            vReturn.push_back(*curItem);
			}
        }

        return vReturn;
}
if (!dup){} will execute only if dup is false. Regardless of what the value was at the time of initializing, AKA dup's current state.
Last edited on
Observe this:
true == !false (true equals NOT false)
false == !true (false equals NOT true)


1
2
3
if (!dup) // is the same as

if (dup == false)

but in the first looping...i mean the first of the for loops... chkItem will be equal to curItem...so the " if " is not statisfied and dup = true will not be executed...
so in that case can i intepret line 14 to be if (dup == true) ???
If you were to always use braces - even when there is only 1 statement, and proper indentation (IDE should do this automatically) - that would mean less confusion:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template <typename T>
std::vector<T> CreateUniqueVector(const T* firstItem, const T* lastItem)
{
std::vector<T> vReturn;

bool dup = false;

	for (const T* curItem = firstItem; curItem != lastItem; ++curItem,dup = false ) {
		for (const T* chkItem = firstItem; chkItem != lastItem && !dup; ++chkItem) {
                             if ((chkItem != curItem) && (*chkItem == *curItem)) {  
			          dup = true;
                             }

		             if (!dup) {
                                 vReturn.push_back(*curItem);
     			     }
        }

        return vReturn;
}


I changed the bracing style, In my personal opinion I think it looks a bit wacky to have opening braces on their own line when there is only 1 statement attached to the if. But other people have their own preferences.

IMO it is a good idea to always use braces in this way because it will save you one day when you add more code, and stops this type of interpretation:

12
13
14
			dup = true; // forgot it is associated with the if

		if (!dup) // will never execute? 



Can you use the std::unique algorithm? Maybe you are not allowed to.

http://en.cppreference.com/w/cpp/algorithm/unique


Hope all goes well :+)

EDIT:

Also if you set you IDE to have tabs as 4 spaces, it will display better on this page.
Last edited on
If not using std::unique along with std::sort, how about this:

6
7
8
9
10
11
12
13
14
15
16
17
18
bool dup = false;

       for (const T* curItem = firstItem; curItem != lastItem; ++curItem,dup = false ) { // should be ==?
		for (const T* chkItem = firstItem; chkItem != lastItem && !dup; ++chkItem) { 
                             if !((chkItem != curItem) && (*chkItem == *curItem)) {  //only duplicates
			          dup = true;
                                   vReturn.push_back(*curItem);
                             }

		             if (!dup) {
                                 vReturn.push_back(*curItem);
     			     }
        }


Also can you use a range based for loop with auto to iterate through all the items?
closed account (iAk3T05o)
@TheIdeasMan:
1
2
3
if (blah == blahh){
// blah
}

is irritating for me.
I prefer
1
2
3
4
if (blah == blahh)
{
//blah
}
@Nathan2222

Sure, no worries. But I did say:

TheIdeasMan wrote:
But other people have their own preferences.


There have been huge (& rather pointless IMO) discussions about this in the past, I was hoping to avoid a repeat. :+D

regards
is ok the ideas man...I've also adopted your way of identation long time ago...which makes reading very long codes with lots of braces really very very easy and is the best...I did not write this code..i just have to intepret it...thanks anyway...
Topic archived. No new replies allowed.