I15 << operator. and hw 16 questions.

The Partridge Family were neither partridges nor a family. Discuss.
Trumps_Nipple
Posts: 27
Joined: August 2nd, 2016, 12:34 am
Location: Russia, SPb
Contact:

I15 << operator. and hw 16 questions.

Post by Trumps_Nipple » November 1st, 2017, 6:18 pm

Greatings traveler!
How do we manage "<< operator" in HW for I15 in foreach loop? This is because in our Stack class we implement Iterator class?
Because I had stucked with error "operand types are : std::ostream << Stack::Element"
when I implemented first task kind of. And I still have it even if I adopted code from jeetsukumaran's github repo.

Give me pls explanations about
// write your custom insertion operator here!
What the hek? It's about whole challenge.

2nd and 3d tasks:
in one statement? are u srs? i can't find a path to begin with...
and the sentence " don't use std::find_if or other searches " just bite me in the dust.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: I15 << operator. and hw 16 questions.

Post by albinopapa » November 2nd, 2017, 1:06 am

This sounds more like I16 homework instead of I15. Intermediate 15 homework was to get Stack to play nice with ranged for loop. Intermediate 16 was about the algorithms.
Give me pls explanations about
// write your custom insertion operator here!
What the hek? It's about whole challenge.
The insertion operator<< can be used to pass in an object same as a Set function or operator=.

Code: Select all

class MyClass
{
public:
	MyClass() = default;
	MyClass( int A, int B, int C )
		:
		a( A ), b( B ), c( C )
	{}
	MyClass &operator<<( const MyClass& Src )
	{
		a = Src.a;
		b = Src.b;
		c = Src.c;

		return *this;
	}

private:
	int a, b, c;
};
The global operator<< can be overloaded using the overload

Code: Select all

std::ostream &operator<<( std::ostream &out, const MyClass& Obj )
{
	out << Obj.GetA() << ", " << Obj.GetB() << ", " << Obj.GetC();

	return out;
}

int main()
{
	const MyClass obj{ 1,2,3 };
	
	stringstream out;
	out << obj;
	cout << "MyClass c object values: " << out.str() << endl;
	while( !_kbhit() );
	return 0;
}
This will insert the values of MyClass to text and print them on the screen.

As for the 2nd and 3rd challenges, I'm having issues figuring out where to begin as well lol. I haven't used all the algorithms in the library yet, so it's all new to me.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: I15 << operator. and hw 16 questions.

Post by chili » November 2nd, 2017, 1:34 am

Trumps_Nipple wrote:Greatings traveler!
How do we manage "<< operator" in HW for I15 in foreach loop? This is because in our Stack class we implement Iterator class?
Because I had stucked with error "operand types are : std::ostream << Stack::Element"
when I implemented first task kind of. And I still have it even if I adopted code from jeetsukumaran's github repo.

Give me pls explanations about
// write your custom insertion operator here!
What the hek? It's about whole challenge.

2nd and 3d tasks:
in one statement? are u srs? i can't find a path to begin with...
and the sentence " don't use std::find_if or other searches " just bite me in the dust.
You seem a little frustrated with the homework assignment. Do you feel that maybe it was a little unfair? Does it make you angry?
Chili

Trumps_Nipple
Posts: 27
Joined: August 2nd, 2016, 12:34 am
Location: Russia, SPb
Contact:

Re: I15 << operator. and hw 16 questions.

Post by Trumps_Nipple » November 2nd, 2017, 9:06 am

albinopapa, nice, saw that way to overload operator but ignored)
Chili, it's not unfair, it's just not trivial and your restrictions like ONE-STATEMENT SOLUTION || DON'T USE THAT OP FUNCTION IN ALGORITHMS have been fucking me since i saw them.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: I15 << operator. and hw 16 questions.

Post by chili » November 2nd, 2017, 9:38 am

For sure, it is not trivial. I mentioned it was to be a challenge homework, not a simple drill.

As for the restrictions, the one-statement solution you mention is not a restriction. Note that I say here that it *can* be done in one statement, not that it *must* be. And the find_if restriction is actually a hint in disguise ;)

Of course, if you can think of no way except one that will break a restriction, go ahead. Better to come up with an invalid solution than no solution at all bruh. And remember, the solution video is always there.
Chili

Trumps_Nipple
Posts: 27
Joined: August 2nd, 2016, 12:34 am
Location: Russia, SPb
Contact:

Re: I15 << operator & HW I16 questions.

Post by Trumps_Nipple » November 3rd, 2017, 3:09 pm

kk. i will write my solutions here provide I'll find them

2nd :

First of all I copy from stackoverflow 2 additional functions for breaking entire string into tokens for dealing with them further:
https://stackoverflow.com/questions/236 ... f-a-string
Answer with 2.1k+ votes.
Here they are:

Code: Select all

template<typename Out>
void split(const std::string &s, char delim, Out result) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		*(result++) = item;
	}
}

std::vector<std::string> split_(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, std::back_inserter(elems));
	return elems;
}
And after goes this :

Code: Select all

{
	// code goes here
	std::vector<std::string> tokens = split_(nambies, ' ');
	for each (std::string str in tokens) {
		auto temp = std::find_if(numbers.begin(), numbers.end(), [str](const Pube pube) {
				return str._Equal(pube.str);
			}
		);
		if (temp != numbers.end()) {
			std::cout << temp->num;
		}
	}
}
I'm waiting for yours corrections and tomatoes on my code)))))

Trumps_Nipple
Posts: 27
Joined: August 2nd, 2016, 12:34 am
Location: Russia, SPb
Contact:

Re: I15 << operator. and hw 16 questions.

Post by Trumps_Nipple » November 3rd, 2017, 4:53 pm

3d one. Can't even imagine how did you do that without searches... (didn't watch the video):

Code: Select all

// code goes here
	for each (int num in numpies) {
		auto temp = std::find_if(numbers.begin(), numbers.end(), [num](const Pube pube) {
			return num == pube.num;
		}
		);
		if (temp != numbers.end()) {
			std::cout << temp->str << ' ';
		}
	}
4th (It looks like 2nd but with some addition stuff):

Code: Select all

int product = 1;
	std::vector<std::string> tokens = split_(nambies, ' ');
	for each (std::string str in tokens) {
		auto temp = std::find_if(numbers.begin(), numbers.end(), [str](const Pube pube) {
			return str._Equal(pube.str);
			}
		);
		if (temp != numbers.end()) {
			product *= temp->num;
		}
	}
	std::cout << product << std::endl;
5th. Maybe I got wrong meaning:

Code: Select all

auto numbIt = numbers.begin();
		auto memeIt = memes.begin();
		for (; numbIt != numbers.end() || memeIt != memes.end(); numbIt++, memeIt++) {
			std::cout << numbIt->num + memeIt->num << ',';
		}
		std::cout << '\b' << " " << std::endl;
6th. Code copied from http://en.cppreference.com/w/cpp/algorithm/remove:

Code: Select all

template<class T, class UnaryPredicate> void remove_erase_if(std::vector<T>& vec, UnaryPredicate p)
{
	auto first = vec.begin();
	auto last = vec.end();
	auto result = first;
	for (; first != last; ++first) {
		if (!p(*first)) {
			*result++ = *first;
		}
	}
	vec.erase(result, last);
}

Trumps_Nipple
Posts: 27
Joined: August 2nd, 2016, 12:34 am
Location: Russia, SPb
Contact:

Re: I15 << operator. and hw 16 questions.

Post by Trumps_Nipple » November 3rd, 2017, 4:57 pm

My soul is free now...
Oh, hello there. I will stay behind, to gaze at the sun. The sun is a wondrous body. Like a magnificent father! If only I could be so grossly incandescent!

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: I15 << operator. and hw 16 questions.

Post by albinopapa » November 3rd, 2017, 7:07 pm

Just a little FYI.

The for loops you have chosen to use are NOT standard C++ and will NOT be portable, which means if you ever decided to use a different compiler you may not have those available and wonder why the compiler is complaining.

for each, in

It turns out that it is a part of the C++/CX language extensions from Microsoft. To iterate over a range in a for loop, the better option is to use

Code: Select all

for( auto& element : elements )
{
}

or 

for( const auto& element : elements )
{
}
Something that I've notices about the for each, in version is the elements returned are always const. Most of the time this is probably what you want, but there are times you are going to want to change an element or call a non-const member function and it won't be allowed in that loop.
Example:

Code: Select all

	for each( int &element in myints )
	{
		if( element > 5 )
		{
			element = 5;
		}
	}
Even though I don't specify const, it is implicitly const and I get a compile time error
1>error C3892: 'element': you cannot assign to a variable that is const
In case you are wondering, when iterating over a container such as a std::vector, you'd want to use a const reference or a reference so you aren't making copies of the elements as you iterate over them.
There's two reasons for wanting to do this. First, if the container holds large objects then it would be time consuming to copy over every element and your program would run slow. Second, if you just made copies of each element any changes you make to that element would be lost.

The second reason has been a point of bugs in a few beginners projects. They use the ranged for loop without using a reference to the elements and call an Update function for instance and wonder why their shit doesn't move on screen. It's because they only changed the copies and not the originals.

Code: Select all

// Use const auto& when just reading values from container
for( const auto& element : elements )

// Use auto& when needing to change elements or call non-const member functions
for( auto& element : elements )
{
     if(element.IsAlive())
     {
          element.Update( dt );
     }
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: I15 << operator. and hw 16 questions.

Post by albinopapa » November 3rd, 2017, 7:14 pm

Since you were suppose to use the algorithms library, you could have used std::remove_if here

Code: Select all

// Your code
template<class T, class UnaryPredicate> void remove_erase_if(std::vector<T>& vec, UnaryPredicate p)
{
   auto first = vec.begin();
   auto last = vec.end();
   auto result = first;
   for (; first != last; ++first) {
      if (!p(*first)) {
         *result++ = *first;
      }
   }
   vec.erase(result, last);
}

// Could be
template<class T, class UnaryPredicate> void remove_erase_if(std::vector<T>& vec, UnaryPredicate p)
{  
     vec.erase( std::remove_if(vec.begin(), vec.end(), p), vec.end() );

     or
     
     auto remIt = std::remove_if( vec.begin(), vec.end(), p );
     vec.erase( remIt, vec.end() );
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply