C++ Weekly - Ep 7 Stop Using std::endl

C++ Weekly With Jason Turner · 75K views · 2016 · 11:20

Open on YouTube

C, C++, Rust, Go & systems languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it in answers as a short reference when they tell askers to print '\n' instead of std::endl in their C++ code.

Recommended by 0 people across 0 different months, 2018 to 2020.

What it is

An 11-minute episode of Jason Turner's C++ Weekly series arguing that C++ programmers should stop using std::endl. It explains that endl does more than write a newline because it also flushes the stream, and shows the cheaper alternative.

What people said

Cites mostsignificant · 2018-11 · Stack Overflow · on “How to create a vector with an initial size by using a custom constructor and without calling the destructor?”

You could solve your problem with a vector of unique_ptr's to the Foo objects. class Foo { public: Foo(int i) {} // ... }; #include <algorithm> #include <vector> auto main() -> int { std::vector<std::unique_ptr<Foo>> foos(100); std::generate(foos.begin(), foos.end(), []() { return std::make_unique<Foo>(5); }); } However, you don't want to do this because otherwise you would have to go the extra indirection with the unique_ptr when calling anything on a vector's Foo object. As others have already suggested, you should store Foo's int members in a vector. Then you can just reserve [LINK] the space in the vector and emplace_back [LINK] (construct) your Foo objects directly in the vector of Foo'

Cites metal · 2019-02 · Stack Overflow · on “2D array won't print properly”

The problem lies in the line: if(j = 9) . You need a double equals to compare. Here, you're assigning to j , which makes it skip the rest of the line. Unsolicited advice: Avoid hard-coding your constants (e.g., 7, 10, 9). Use sizeof for arrays, or at least a constant value like const auto rows = 7; . If you get much more complex or bigger maps, you should probably move away from raw arrays to std::vector or perhaps std::array . If you do this, you can solve #1 by using myGrid.size() and myGrid[row].size() . Use '\n' rather than std::endl [this video] .

Cites Rokas Višinskas · 2019-05 · Stack Overflow · on “How to update the attributes of an object?”

You can easily modify non-const object values by using the . operator. myAircraft.altitude = newAltitude; myAircraft.speed = newSpeed; myAircraft.direction = newDirection; cout << myAircraft.altitude << ", " << myAircraft.speed << ", " << myAircraft.direction << '\n'; P.S. It's a very bad practice to print strings by concatenating them with + . Instead, use the << operator as shown above. In your case, the code is not even valid since you're trying to add a number with a string. The logically correct way to do what you intended would be to convert the number into a string first to_string(myAircraft.altitude) + ", " . Also, avoid using endl since it unnecessarily flushes the buffer. The benef

Cites Rokas Višinskas · 2019-09 · Stack Overflow · on “Search vector of struct for user inputted string”

You are using string::find instead of just comparing a string with a string. Assuming entity is of type string . The following code should work fine. bool found = false; for(int i = 0; i < Oscars.size(); i++){ if(Oscars[i].entity == input){ cout << "Found at " << i << '\n'; found = true; break; } } Some points: Kindly consider not using endl. [this video] You don't need the safety of vector::at when iterating based on i < vec.size() .

Cites Rokas Višinskas · 2019-09 · Stack Overflow · on “Exception thrown at 0x5914F3BE (ucrtbased.dll)”

Your problems begin with the wrong amount written in your data file. Fix it with: 2 Bob 400 Alice 11000 They then continue with the fact that you inccorectly read the file. Remember: Mixing operator>> and getline() is not as simple as it seems. You see, operator>> IGNORES newline and space characters until it finds any other character. It then reads the upcoming characters until it encounters the next newline or space character, BUT DOES NOT DISCARD IT. Here is where the problem with getline comes in. getline reads EVERYTHING until it encounters newline or a specified delim character. Meaning, that if your operator>> stops after encountering newline , getline will read NOTHING since it immed

Cites metal · 2020-03 · Stack Overflow · on “can write file with zlib but cannot read it back”

Here is a version that does approximately what you're asking for, but using standard file streams, not the non-standard zstr library which doesn't seem essential here: #include <iostream> #include <fstream> #include <memory> #include <string> #include <vector> using namespace std::string_literals; int main() { constexpr auto fileName = "test.bin"; { const auto str = "hello world"s; auto ofs = std::ofstream( fileName, std::ios::binary ); ofs.write( str.data(), str.size() ); } // ofs is closed here by RAII auto buff = std::vector<char>(100, 'A'); auto ifs = std::ifstream( fileName, std::ios::binary ); ifs.read(buff.data(), buff.size()); std::cout << std::string(buff.data(), buff.data()+11) <<