mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2026-09-24 15:00:31 +08:00
Replace std::endl with \n
This commit is contained in:
+1
-1
@@ -25,7 +25,7 @@ int main() {
|
||||
MyNamespace::MyClass obj;
|
||||
obj.value = 42;
|
||||
using std::cout; // Required to use 'cout' without fully qualifying it.
|
||||
cout << obj << std::endl; // ADL is used to find the correct overloaded 'operator<<'.
|
||||
cout << obj << '\n'; // ADL is used to find the correct overloaded 'operator<<'.
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
+2
-2
@@ -24,12 +24,12 @@ int main() {
|
||||
|
||||
// Without auto, iterating the vector would look like this:
|
||||
for (std::vector<int>::iterator it = myVector.begin(); it != myVector.end(); ++it) {
|
||||
std::cout << *it << std::endl;
|
||||
std::cout << *it << '\n';
|
||||
}
|
||||
|
||||
// With auto, the iterator declaration becomes simpler:
|
||||
for (auto it = myVector.begin(); it != myVector.end(); ++it) {
|
||||
std::cout << *it << std::endl;
|
||||
std::cout << *it << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -16,7 +16,7 @@ Some of the notable features in C++11 include:
|
||||
```cpp
|
||||
std::vector<int> vec = {1, 2, 3};
|
||||
for (int i : vec) {
|
||||
std::cout << i << std::endl;
|
||||
std::cout << i << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ The C++11 standard, also known as C++0x, was officially released in September 20
|
||||
```cpp
|
||||
std::vector<int> numbers {1, 2, 3, 4};
|
||||
for (int number : numbers) {
|
||||
std::cout << number << std::endl;
|
||||
std::cout << number << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ std::future<int> async_value(int value) {
|
||||
|
||||
int main() {
|
||||
auto result = async_value(42);
|
||||
std::cout << "Result: " << result.get() << std::endl;
|
||||
std::cout << "Result: " << result.get() << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ int main() {
|
||||
class HelloWorld {
|
||||
public:
|
||||
void printHello() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
std::cout << "Hello, World!" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ The first stage is the preprocessing of the source code. Preprocessors modify th
|
||||
#define PI 3.14
|
||||
|
||||
int main() {
|
||||
std::cout << "The value of PI is: " << PI << std::endl;
|
||||
std::cout << "The value of PI is: " << PI << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -22,7 +22,7 @@ Let's say you have a simple C++ program saved in a file called `hello.cpp`:
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
std::cout << "Hello, World!" << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -19,10 +19,10 @@ void modifyVariable(int* ptr) {
|
||||
int main() {
|
||||
const int original_value = 10;
|
||||
int* non_const_value_ptr = const_cast<int*>(&original_value);
|
||||
std::cout << "Original value: " << original_value << std::endl;
|
||||
std::cout << "Original value: " << original_value << '\n';
|
||||
|
||||
modifyVariable(non_const_value_ptr);
|
||||
std::cout << "Modified value: " << *non_const_value_ptr << ", original_value: " << original_value << std::endl;
|
||||
std::cout << "Modified value: " << *non_const_value_ptr << ", original_value: " << original_value << '\n';
|
||||
|
||||
assert(non_const_value_ptr == &original_value);
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ int main() {
|
||||
for (int x : vec) {
|
||||
std::cout << ' ' << x;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -44,7 +44,7 @@ int main() {
|
||||
for (int x : lst) {
|
||||
std::cout << ' ' << x;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -64,9 +64,9 @@ int main() {
|
||||
m["one"] = 1;
|
||||
m["two"] = 2;
|
||||
|
||||
std::cout << "Map contains:" << std::endl;
|
||||
std::cout << "Map contains:" << '\n';
|
||||
for (const auto &pair : m) {
|
||||
std::cout << pair.first << ": " << pair.second << std::endl;
|
||||
std::cout << pair.first << ": " << pair.second << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -87,9 +87,9 @@ int main() {
|
||||
um["one"] = 1;
|
||||
um["two"] = 2;
|
||||
|
||||
std::cout << "Unordered map contains:" << std::endl;
|
||||
std::cout << "Unordered map contains:" << '\n';
|
||||
for (const auto &pair : um) {
|
||||
std::cout << pair.first << ": " << pair.second << std::endl;
|
||||
std::cout << pair.first << ": " << pair.second << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -14,7 +14,7 @@ public:
|
||||
|
||||
// Use the same shared data for copying.
|
||||
MyString(const MyString &other) : data(other.data) {
|
||||
std::cout << "Copied using the Copy-Write idiom." << std::endl;
|
||||
std::cout << "Copied using the Copy-Write idiom." << '\n';
|
||||
}
|
||||
|
||||
// Make a copy only if we want to modify the data.
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
// Check if there's more than one reference.
|
||||
if (data.use_count() > 1) {
|
||||
data = std::make_shared<std::string>(*data);
|
||||
std::cout << "Copy is actually made for writing." << std::endl;
|
||||
std::cout << "Copy is actually made for writing." << '\n';
|
||||
}
|
||||
*data = str;
|
||||
}
|
||||
|
||||
@@ -17,14 +17,14 @@ public:
|
||||
}
|
||||
|
||||
void implementation() {
|
||||
std::cout << "Default implementation in Base" << std::endl;
|
||||
std::cout << "Default implementation in Base" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
class Derived1 : public Base<Derived1> {
|
||||
public:
|
||||
void implementation() {
|
||||
std::cout << "Custom implementation in Derived1" << std::endl;
|
||||
std::cout << "Custom implementation in Derived1" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ public:
|
||||
int age;
|
||||
|
||||
void printInfo() {
|
||||
std::cout << "Name: " << name << ", Age: " << age << std::endl;
|
||||
std::cout << "Name: " << name << ", Age: " << age << '\n';
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ To convert a time point to calendar representation, you can use the `std::chrono
|
||||
int main() {
|
||||
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
||||
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
|
||||
std::cout << "Current time: " << std::ctime(&now_c) << std::endl;
|
||||
std::cout << "Current time: " << std::ctime(&now_c) << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -12,28 +12,28 @@ To resolve this ambiguity, you can use virtual inheritance. A virtual base class
|
||||
class Base {
|
||||
public:
|
||||
void print() {
|
||||
std::cout << "Base class" << std::endl;
|
||||
std::cout << "Base class" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
class Derived1 : virtual public Base {
|
||||
public:
|
||||
void derived1Print() {
|
||||
std::cout << "Derived1 class" << std::endl;
|
||||
std::cout << "Derived1 class" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
class Derived2 : virtual public Base {
|
||||
public:
|
||||
void derived2Print() {
|
||||
std::cout << "Derived2 class" << std::endl;
|
||||
std::cout << "Derived2 class" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
class Derived3 : public Derived1, public Derived2 {
|
||||
public:
|
||||
void derived3Print() {
|
||||
std::cout << "Derived3 class" << std::endl;
|
||||
std::cout << "Derived3 class" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ Here's an example in C++ demonstrating dynamic polymorphism.
|
||||
class Shape {
|
||||
public:
|
||||
virtual void draw() {
|
||||
std::cout << "Drawing a shape" << std::endl;
|
||||
std::cout << "Drawing a shape" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
class Circle : public Shape {
|
||||
public:
|
||||
void draw() override {
|
||||
std::cout << "Drawing a circle" << std::endl;
|
||||
std::cout << "Drawing a circle" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -31,7 +31,7 @@ public:
|
||||
class Rectangle : public Shape {
|
||||
public:
|
||||
void draw() override {
|
||||
std::cout << "Drawing a rectangle" << std::endl;
|
||||
std::cout << "Drawing a rectangle" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -20,13 +20,13 @@ int main() {
|
||||
void* void_ptr;
|
||||
|
||||
void_ptr = &x;
|
||||
std::cout << "int value: " << *(static_cast<int*>(void_ptr)) << std::endl;
|
||||
std::cout << "int value: " << *(static_cast<int*>(void_ptr)) << '\n';
|
||||
|
||||
void_ptr = &y;
|
||||
std::cout << "float value: " << *(static_cast<float*>(void_ptr)) << std::endl;
|
||||
std::cout << "float value: " << *(static_cast<float*>(void_ptr)) << '\n';
|
||||
|
||||
void_ptr = &z;
|
||||
std::cout << "string value: " << *(static_cast<std::string*>(void_ptr)) << std::endl;
|
||||
std::cout << "string value: " << *(static_cast<std::string*>(void_ptr)) << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -45,13 +45,13 @@ int main() {
|
||||
std::any any_value;
|
||||
|
||||
any_value = 42;
|
||||
std::cout << "int value: " << std::any_cast<int>(any_value) << std::endl;
|
||||
std::cout << "int value: " << std::any_cast<int>(any_value) << '\n';
|
||||
|
||||
any_value = 3.14;
|
||||
std::cout << "double value: " << std::any_cast<double>(any_value) << std::endl;
|
||||
std::cout << "double value: " << std::any_cast<double>(any_value) << '\n';
|
||||
|
||||
any_value = std::string("Hello, world!");
|
||||
std::cout << "string value: " << std::any_cast<std::string>(any_value) << std::endl;
|
||||
std::cout << "string value: " << std::any_cast<std::string>(any_value) << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -10,14 +10,14 @@ Here is a basic example of how `dynamic_cast` can be used:
|
||||
class BaseClass {
|
||||
public:
|
||||
virtual void display() {
|
||||
std::cout << "BaseClass" << std::endl;
|
||||
std::cout << "BaseClass" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
class DerivedClass : public BaseClass {
|
||||
public:
|
||||
void display() {
|
||||
std::cout << "DerivedClass" << std::endl;
|
||||
std::cout << "DerivedClass" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -31,9 +31,9 @@ int main() {
|
||||
|
||||
try {
|
||||
int result = divide(num1, num2);
|
||||
std::cout << "The result is: " << result << std::endl;
|
||||
std::cout << "The result is: " << result << '\n';
|
||||
} catch (const char* msg) {
|
||||
std::cerr << "Error: " << msg << std::endl;
|
||||
std::cerr << "Error: " << msg << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -71,9 +71,9 @@ int main() {
|
||||
|
||||
try {
|
||||
int result = divide(num1, num2);
|
||||
std::cout << "The result is: " << result << std::endl;
|
||||
std::cout << "The result is: " << result << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
std::cerr << "Error: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -39,11 +39,11 @@ try {
|
||||
throw "Division by zero not allowed!";
|
||||
} else {
|
||||
int result = num1 / num2;
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
std::cout << "Result: " << result << '\n';
|
||||
}
|
||||
}
|
||||
catch (const char* e) {
|
||||
std::cout << "Error: " << e << std::endl;
|
||||
std::cout << "Error: " << e << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -15,14 +15,14 @@ int main() {
|
||||
// Some code here...
|
||||
|
||||
if (/*some error condition*/) {
|
||||
std::cout << "An error occurred." << std::endl;
|
||||
std::cout << "An error occurred." << '\n';
|
||||
return 1;
|
||||
}
|
||||
|
||||
// More code here...
|
||||
|
||||
if (/*another error condition*/) {
|
||||
std::cout << "Another error occurred." << std::endl;
|
||||
std::cout << "Another error occurred." << '\n';
|
||||
return 2;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ void some_function() {
|
||||
// Some code here...
|
||||
|
||||
if (/*some error condition*/) {
|
||||
std::cout << "An error occurred." << std::endl;
|
||||
std::cout << "An error occurred." << '\n';
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ For example:
|
||||
|
||||
int main() {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
std::cout << "Iteration: " << i << std::endl;
|
||||
std::cout << "Iteration: " << i << '\n';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -47,7 +47,7 @@ For example:
|
||||
int main() {
|
||||
int i = 0;
|
||||
while (i < 5) {
|
||||
std::cout << "Iteration: " << i << std::endl;
|
||||
std::cout << "Iteration: " << i << '\n';
|
||||
i++;
|
||||
}
|
||||
return 0;
|
||||
@@ -74,7 +74,7 @@ For example:
|
||||
int main() {
|
||||
int i = 0;
|
||||
do {
|
||||
std::cout << "Iteration: " << i << std::endl;
|
||||
std::cout << "Iteration: " << i << '\n';
|
||||
i++;
|
||||
} while (i < 5);
|
||||
return 0;
|
||||
|
||||
@@ -19,7 +19,7 @@ template <typename T>
|
||||
class MyContainer {
|
||||
public:
|
||||
void print() {
|
||||
std::cout << "Generic container." << std::endl;
|
||||
std::cout << "Generic container." << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -28,7 +28,7 @@ template <>
|
||||
class MyContainer<int> {
|
||||
public:
|
||||
void print() {
|
||||
std::cout << "Container for integers." << std::endl;
|
||||
std::cout << "Container for integers." << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ int addNumbers(int a, int b) {
|
||||
int main() {
|
||||
int num1 = 5, num2 = 10;
|
||||
int result = addNumbers(num1, num2); // Calling the function
|
||||
std::cout << "The sum is: " << result << std::endl;
|
||||
std::cout << "The sum is: " << result << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -58,7 +58,7 @@ int multiplyNumbers(int x, int y);
|
||||
int main() {
|
||||
int num1 = 3, num2 = 7;
|
||||
int result = multiplyNumbers(num1, num2); // Calling the function
|
||||
std::cout << "The product is: " << result << std::endl;
|
||||
std::cout << "The product is: " << result << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ Suppose you have a simple `cpp` file called `example.cpp`:
|
||||
#include <iostream>
|
||||
|
||||
void my_function(int i) {
|
||||
std::cout << "In my_function with i = " << i << std::endl;
|
||||
std::cout << "In my_function with i = " << i << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
@@ -33,7 +33,7 @@ Example of a source file:
|
||||
#include <iostream>
|
||||
|
||||
void Example::printMessage() {
|
||||
std::cout << "Hello, code splitting!" << std::endl;
|
||||
std::cout << "Hello, code splitting!" << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ int main() {
|
||||
int number;
|
||||
std::cout << "Enter an integer: ";
|
||||
std::cin >> number;
|
||||
std::cout << "You entered: " << number << std::endl;
|
||||
std::cout << "You entered: " << number << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -124,7 +124,7 @@ int add(int a, int b) {
|
||||
|
||||
int main() {
|
||||
int result = add(3, 4);
|
||||
std::cout << "3 + 4 = " << result << std::endl;
|
||||
std::cout << "3 + 4 = " << result << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -26,7 +26,7 @@ int main() {
|
||||
int a;
|
||||
std::cout << "Enter a number: ";
|
||||
std::cin >> a;
|
||||
std::cout << "You entered: " << a << std::endl;
|
||||
std::cout << "You entered: " << a << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -35,8 +35,8 @@ int main() {
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cerr << "An error occurred." << std::endl;
|
||||
std::clog << "Logging information." << std::endl;
|
||||
std::cerr << "An error occurred." << '\n';
|
||||
std::clog << "Logging information." << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -25,7 +25,7 @@ Here are a few examples to demonstrate the use of lambda functions in C++:
|
||||
|
||||
```cpp
|
||||
auto printHello = []() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
std::cout << "Hello, World!" << '\n';
|
||||
};
|
||||
printHello(); // Output: Hello, World!
|
||||
```
|
||||
|
||||
@@ -13,7 +13,7 @@ C++ provides the following logical operators:
|
||||
```cpp
|
||||
int a = 5, b = 10;
|
||||
if (a > 0 && b > 0) {
|
||||
std::cout << "Both values are positive." << std::endl;
|
||||
std::cout << "Both values are positive." << '\n';
|
||||
}
|
||||
```
|
||||
- **OR Operator (||)**
|
||||
@@ -25,7 +25,7 @@ C++ provides the following logical operators:
|
||||
```cpp
|
||||
int a = 5, b = -10;
|
||||
if (a > 0 || b > 0) {
|
||||
std::cout << "At least one value is positive." << std::endl;
|
||||
std::cout << "At least one value is positive." << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -38,7 +38,7 @@ C++ provides the following logical operators:
|
||||
```cpp
|
||||
int a = 5;
|
||||
if (!(a < 0)) {
|
||||
std::cout << "The value is not negative." << std::endl;
|
||||
std::cout << "The value is not negative." << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -48,7 +48,7 @@ Using these operators, you can create more complex logical expressions, for exam
|
||||
int a = 5, b = -10, c = 15;
|
||||
|
||||
if (a > 0 && (b > 0 || c > 0)) {
|
||||
std::cout << "At least two values are positive." << std::endl;
|
||||
std::cout << "At least two values are positive." << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ class Animal
|
||||
public:
|
||||
void eat()
|
||||
{
|
||||
std::cout << "I can eat!" << std::endl;
|
||||
std::cout << "I can eat!" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -40,7 +40,7 @@ class Mammal
|
||||
public:
|
||||
void breath()
|
||||
{
|
||||
std::cout << "I can breathe!" << std::endl;
|
||||
std::cout << "I can breathe!" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -50,7 +50,7 @@ class Dog : public Animal, public Mammal
|
||||
public:
|
||||
void bark()
|
||||
{
|
||||
std::cout << "I can bark! Woof woof!" << std::endl;
|
||||
std::cout << "I can bark! Woof woof!" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ To create a new thread, include the `<thread>` header file and create an instanc
|
||||
#include <thread>
|
||||
|
||||
void my_function() {
|
||||
std::cout << "This function is executing in a separate thread" << std::endl;
|
||||
std::cout << "This function is executing in a separate thread" << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -32,7 +32,7 @@ You can pass arguments to the thread function by providing them as additional ar
|
||||
#include <thread>
|
||||
|
||||
void print_sum(int a, int b) {
|
||||
std::cout << "The sum is: " << a + b << std::endl;
|
||||
std::cout << "The sum is: " << a + b << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -59,7 +59,7 @@ void print_block(int n, char c) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::cout << c;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,8 +27,8 @@ namespace animals {
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Dog's name: " << animals::dog << std::endl;
|
||||
std::cout << "Cat's name: " << animals::cat << std::endl;
|
||||
std::cout << "Dog's name: " << animals::dog << '\n';
|
||||
std::cout << "Cat's name: " << animals::cat << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -50,8 +50,8 @@ namespace outer {
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Outer x: " << outer::x << std::endl;
|
||||
std::cout << "Inner y: " << outer::inner::y << std::endl;
|
||||
std::cout << "Outer x: " << outer::x << '\n';
|
||||
std::cout << "Inner y: " << outer::inner::y << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -74,7 +74,7 @@ namespace animals {
|
||||
int main() {
|
||||
using animals::dog;
|
||||
|
||||
std::cout << "Dog's name: " << dog << std::endl;
|
||||
std::cout << "Dog's name: " << dog << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -93,8 +93,8 @@ namespace animals {
|
||||
int main() {
|
||||
using namespace animals;
|
||||
|
||||
std::cout << "Dog's name: " << dog << std::endl;
|
||||
std::cout << "Cat's name: " << cat << std::endl;
|
||||
std::cout << "Dog's name: " << dog << '\n';
|
||||
std::cout << "Cat's name: " << cat << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public:
|
||||
int age;
|
||||
|
||||
void bark() {
|
||||
std::cout << name << " barks!" << std::endl;
|
||||
std::cout << name << " barks!" << '\n';
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
}
|
||||
|
||||
void bark() {
|
||||
std::cout << name << " barks!" << std::endl;
|
||||
std::cout << name << " barks!" << '\n';
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -62,14 +62,14 @@ Inheritance is the concept of deriving new classes from existing ones, which ena
|
||||
class Animal {
|
||||
public:
|
||||
void breathe() {
|
||||
std::cout << "I can breathe" << std::endl;
|
||||
std::cout << "I can breathe" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
class Dog : public Animal {
|
||||
public:
|
||||
void bark() {
|
||||
std::cout << "Dog barks!" << std::endl;
|
||||
std::cout << "Dog barks!" << '\n';
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -90,21 +90,21 @@ Polymorphism allows you to use a single interface to represent different types.
|
||||
class Animal {
|
||||
public:
|
||||
virtual void makeSound() {
|
||||
std::cout << "The Animal makes a sound" << std::endl;
|
||||
std::cout << "The Animal makes a sound" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
class Dog : public Animal {
|
||||
public:
|
||||
void makeSound() override {
|
||||
std::cout << "Dog barks!" << std::endl;
|
||||
std::cout << "Dog barks!" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
class Cat : public Animal {
|
||||
public:
|
||||
void makeSound() override {
|
||||
std::cout << "Cat meows!" << std::endl;
|
||||
std::cout << "Cat meows!" << '\n';
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
+3
-3
@@ -36,9 +36,9 @@ int main() {
|
||||
MyTemplate<double*> t2; // Partial specialization for pointers
|
||||
MyTemplate<int> t3; // Full specialization for int
|
||||
|
||||
std::cout << t1.name() << std::endl;
|
||||
std::cout << t2.name() << std::endl;
|
||||
std::cout << t3.name() << std::endl;
|
||||
std::cout << t1.name() << '\n';
|
||||
std::cout << t2.name() << '\n';
|
||||
std::cout << t3.name() << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ class MyClass_Impl // the actual implementation
|
||||
public:
|
||||
void some_method()
|
||||
{
|
||||
std::cout << "Implementation method called!" << std::endl;
|
||||
std::cout << "Implementation method called!" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -14,10 +14,10 @@ You can use the reference just like you'd use the original variable. When you ch
|
||||
|
||||
```cpp
|
||||
var = 20; // Sets the value of var to 20
|
||||
std::cout << ref << std::endl; // Outputs 20
|
||||
std::cout << ref << '\n'; // Outputs 20
|
||||
|
||||
ref = 30; // Sets the value of ref to 30
|
||||
std::cout << var << std::endl; // Outputs 30
|
||||
std::cout << var << '\n'; // Outputs 30
|
||||
```
|
||||
|
||||
## Function Parameters
|
||||
@@ -31,9 +31,9 @@ void swap(int& a, int& b) {
|
||||
|
||||
int main() {
|
||||
int x = 5, y = 10;
|
||||
std::cout << "Before Swap: x = " << x << " y = " << y << std::endl; // Outputs 5 10
|
||||
std::cout << "Before Swap: x = " << x << " y = " << y << '\n'; // Outputs 5 10
|
||||
|
||||
swap(x, y);
|
||||
std::cout << "After Swap: x = " << x << " y = " << y << std::endl; // Outputs 10 5
|
||||
std::cout << "After Swap: x = " << x << " y = " << y << '\n'; // Outputs 10 5
|
||||
}
|
||||
```
|
||||
|
||||
@@ -18,7 +18,7 @@ int main() {
|
||||
|
||||
for (size_t i = 0; i < sizeof(int); ++i) {
|
||||
// Print the individual bytes of the integer as characters
|
||||
std::cout << "Byte " << i << ": " << char_ptr[i] << std::endl;
|
||||
std::cout << "Byte " << i << ": " << char_ptr[i] << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -10,7 +10,7 @@ The first program that most people learn to write in any programming language is
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
std::cout << "Hello, World!" << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -40,12 +40,12 @@ int main() {
|
||||
To output text to the console, we use the `std::cout` object and the insertion operator `<<`. In the "Hello, World!" example, we used the following line to print "Hello, World!" to the console:
|
||||
|
||||
```cpp
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
std::cout << "Hello, World!" << '\n';
|
||||
```
|
||||
- `std`: This is the namespace where C++ standard library entities (classes and functions) reside. It stands for "standard" and is an abbreviation for the Standard Template Library (STL).
|
||||
- `std::cout`: The standard "character output" stream that writes to the console
|
||||
- `"Hello, World!"`: The string literal to print
|
||||
- `std::endl`: The "end line" manipulator that inserts a newline character and flushes the output buffer
|
||||
- `'\n'`: The "end line" manipulator that inserts a newline character and flushes the output buffer
|
||||
|
||||
## Return Statement
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
int globalVar; // This is a global variable
|
||||
|
||||
int main() {
|
||||
std::cout << "Global variable: " << globalVar << std::endl;
|
||||
std::cout << "Global variable: " << globalVar << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -22,12 +22,12 @@ int main() {
|
||||
void localExample() {
|
||||
int localVar; // This is a local variable
|
||||
localVar = 5;
|
||||
std::cout << "Local variable: " << localVar << std::endl;
|
||||
std::cout << "Local variable: " << localVar << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
localExample();
|
||||
// std::cout << localVar << std::endl; //error: ‘localVar’ was not declared in this scope
|
||||
// std::cout << localVar << '\n'; //error: ‘localVar’ was not declared in this scope
|
||||
}
|
||||
```
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace MyNamespace {
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Namespace variable: " << MyNamespace::namespaceVar << std::endl;
|
||||
std::cout << "Namespace variable: " << MyNamespace::namespaceVar << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -62,8 +62,8 @@ int MyClass::staticMember = 7;
|
||||
|
||||
int main() {
|
||||
MyClass obj(10);
|
||||
std::cout << "Static member: " << MyClass::staticMember << std::endl;
|
||||
std::cout << "Non-static member: " << obj.nonStaticMember << std::endl;
|
||||
std::cout << "Static member: " << MyClass::staticMember << '\n';
|
||||
std::cout << "Non-static member: " << obj.nonStaticMember << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ Create a new file called `main.cpp` within your project and include this code:
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
std::cout << "Hello, World!" << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -15,14 +15,14 @@ Here's an example that demonstrates SFINAE in action:
|
||||
template <typename T, typename = void>
|
||||
struct foo_impl {
|
||||
void operator()(T t) {
|
||||
std::cout << "Called when T is not arithmetic" << std::endl;
|
||||
std::cout << "Called when T is not arithmetic" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct foo_impl<T, std::enable_if_t<std::is_arithmetic<T>::value>> {
|
||||
void operator()(T t) {
|
||||
std::cout << "Called when T is arithmetic" << std::endl;
|
||||
std::cout << "Called when T is arithmetic" << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ Here's a brief summary of the different C++ standards released to date:
|
||||
```cpp
|
||||
std::vector<int> numbers = {1, 2, 3, 4};
|
||||
for (int num : numbers) {
|
||||
std::cout << num << std::endl;
|
||||
std::cout << num << '\n';
|
||||
}
|
||||
```
|
||||
- Smart pointers like `std::shared_ptr` and `std::unique_ptr`.
|
||||
|
||||
@@ -12,15 +12,15 @@ Example:
|
||||
#include <iostream>
|
||||
|
||||
void print(int i) {
|
||||
std::cout << "Printing int: " << i << std::endl;
|
||||
std::cout << "Printing int: " << i << '\n';
|
||||
}
|
||||
|
||||
void print(double d) {
|
||||
std::cout << "Printing double: " << d << std::endl;
|
||||
std::cout << "Printing double: " << d << '\n';
|
||||
}
|
||||
|
||||
void print(const char* s) {
|
||||
std::cout << "Printing string: " << s << std::endl;
|
||||
std::cout << "Printing string: " << s << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -44,7 +44,7 @@ Example:
|
||||
// Template function to print any type
|
||||
template<typename T>
|
||||
void print(const T& value) {
|
||||
std::cout << "Printing value: " << value << std::endl;
|
||||
std::cout << "Printing value: " << value << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
@@ -17,9 +17,9 @@ int main() {
|
||||
c = num; // This asssigment would convert num's value to ASCII equivalent character
|
||||
num = pi; // This assignment would convert pi's value from double type to int type
|
||||
|
||||
std::cout << "The value of num is: " << num << std::endl;
|
||||
std::cout << "The value of pi is: " << pi << std::endl;
|
||||
std::cout << "The value of c is: "<< c << std::endl;
|
||||
std::cout << "The value of num is: " << num << '\n';
|
||||
std::cout << "The value of pi is: " << pi << '\n';
|
||||
std::cout << "The value of c is: "<< c << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -48,7 +48,7 @@ public:
|
||||
void display() {
|
||||
std::cout << "Roll no: " << roll_no
|
||||
<< "\nName: " << name
|
||||
<< "\nMarks: " << marks << std::endl;
|
||||
<< "\nMarks: " << marks << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -19,12 +19,12 @@ Here is an example:
|
||||
|
||||
template <typename T>
|
||||
void printData(const T& data) {
|
||||
std::cout << "General template: " << data << std::endl;
|
||||
std::cout << "General template: " << data << '\n';
|
||||
}
|
||||
|
||||
template <>
|
||||
void printData(const char* const & data) {
|
||||
std::cout << "Specialized template for const char*: " << data << std::endl;
|
||||
std::cout << "Specialized template for const char*: " << data << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -50,7 +50,7 @@ public:
|
||||
MyPair(K k, V v) : key(k), value(v) {}
|
||||
|
||||
void print() const {
|
||||
std::cout << "General template: key = " << key << ", value = " << value << std::endl;
|
||||
std::cout << "General template: key = " << key << ", value = " << value << '\n';
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -65,7 +65,7 @@ public:
|
||||
|
||||
void print() const {
|
||||
std::cout << "Partial specialization for int values: key = " << key
|
||||
<< ", value = " << value << std::endl;
|
||||
<< ", value = " << value << '\n';
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -21,8 +21,8 @@ int main() {
|
||||
int a;
|
||||
int* a_ptr = &a;
|
||||
|
||||
std::cout << "Is 'a' a pointer? " << std::boolalpha << std::is_pointer<decltype(a)>::value << std::endl;
|
||||
std::cout << "Is 'a_ptr' a pointer? " << std::boolalpha << std::is_pointer<decltype(a_ptr)>::value << std::endl;
|
||||
std::cout << "Is 'a' a pointer? " << std::boolalpha << std::is_pointer<decltype(a)>::value << '\n';
|
||||
std::cout << "Is 'a_ptr' a pointer? " << std::boolalpha << std::is_pointer<decltype(a_ptr)>::value << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -46,7 +46,7 @@ typename std::enable_if<std::is_arithmetic<T>::value, T>::type find_max(T a, T b
|
||||
|
||||
int main() {
|
||||
int max = find_max(10, 20);
|
||||
std::cout << "Max: " << max << std::endl;
|
||||
std::cout << "Max: " << max << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ int main() {
|
||||
int num2 = 0;
|
||||
int result = num1 / num2;
|
||||
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
std::cout << "Result: " << result << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ T sum(T t, Args... args) {
|
||||
|
||||
int main() {
|
||||
int result = sum(1, 2, 3, 4, 5); // expands to 1 + 2 + 3 + 4 + 5
|
||||
std::cout << "The sum is: " << result << std::endl;
|
||||
std::cout << "The sum is: " << result << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -63,7 +63,7 @@ class Tuple<Head, Tail...> : public Tuple<Tail...> {
|
||||
|
||||
int main() {
|
||||
Tuple<int, float, double> tuple(1, 2.0f, 3.0);
|
||||
std::cout << "First element: " << tuple.head() << std::endl;
|
||||
std::cout << "First element: " << tuple.head() << '\n';
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -32,12 +32,12 @@ int main() {
|
||||
|
||||
// Using the standalone function 'add'
|
||||
int sum = add(x, y);
|
||||
std::cout << "Sum: " << sum << std::endl;
|
||||
std::cout << "Sum: " << sum << '\n';
|
||||
|
||||
// Using a class and member function
|
||||
Calculator calc;
|
||||
int product = calc.multiply(x, y);
|
||||
std::cout << "Product: " << product << std::endl;
|
||||
std::cout << "Product: " << product << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ int main() {
|
||||
// High-level programming
|
||||
std::vector<int> myVector = {1, 2, 3};
|
||||
for (const auto &i: myVector) {
|
||||
std::cout << i << std::endl;
|
||||
std::cout << i << '\n';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user