Page 1 of 2 [ 12 posts ] Go to page 1, 2  Next

#### I15 << operator. and hw 16 questions.

 Print view Previous topic | Next topic

#### I15 << operator. and hw 16 questions.

Author Message
 Post subject: I15 << operator. and hw 16 questions.  Posted: November 1st, 2017, 6:18 pm

Joined: August 2nd, 2016, 12:34 am
Posts: 27
Location: Russia, SPb
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.

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

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.

 Post subject: Re: I15 << operator. and hw 16 questions.  Posted: November 2nd, 2017, 1:06 am

Joined: February 28th, 2013, 3:23 am
Posts: 2962
Location: Oklahoma, United States
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.

Quote:
// 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:
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;
};

Code:
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

 Post subject: Re: I15 << operator. and hw 16 questions.  Posted: November 2nd, 2017, 1:34 am

Joined: December 31st, 2011, 4:53 pm
Posts: 3537
Location: Japan
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.

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

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

 Post subject: Re: I15 << operator. and hw 16 questions.  Posted: November 2nd, 2017, 9:06 am

Joined: August 2nd, 2016, 12:34 am
Posts: 27
Location: Russia, SPb
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.

 Post subject: Re: I15 << operator. and hw 16 questions.  Posted: November 2nd, 2017, 9:38 am

Joined: December 31st, 2011, 4:53 pm
Posts: 3537
Location: Japan
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

 Post subject: Re: I15 << operator & HW I16 questions.  Posted: November 3rd, 2017, 3:09 pm

Joined: August 2nd, 2016, 12:34 am
Posts: 27
Location: Russia, SPb
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/236129/the-most-elegant-way-to-iterate-the-words-of-a-string
Here they are:
Code:
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:
{
// 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)))))

 Post subject: Re: I15 << operator. and hw 16 questions.  Posted: November 3rd, 2017, 4:53 pm

Joined: August 2nd, 2016, 12:34 am
Posts: 27
Location: Russia, SPb
3d one. Can't even imagine how did you do that without searches... (didn't watch the video):
Code:
// 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:
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:
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:
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);
}

 Post subject: Re: I15 << operator. and hw 16 questions.  Posted: November 3rd, 2017, 4:57 pm

Joined: August 2nd, 2016, 12:34 am
Posts: 27
Location: Russia, SPb
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!

 Post subject: Re: I15 << operator. and hw 16 questions.  Posted: November 3rd, 2017, 7:07 pm

Joined: February 28th, 2013, 3:23 am
Posts: 2962
Location: Oklahoma, United States
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:
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:
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
Quote:
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:
// 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

 Post subject: Re: I15 << operator. and hw 16 questions.  Posted: November 3rd, 2017, 7:14 pm

Joined: February 28th, 2013, 3:23 am
Posts: 2962
Location: Oklahoma, United States
Since you were suppose to use the algorithms library, you could have used std::remove_if here
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

Display posts from previous:  Sort by

 Page 1 of 2 [ 12 posts ] Go to page 1, 2  Next

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: