Programming Articles - Page 3199 of 3363

C++ vs C++0x vs C++11 vs C++98

Smita Kapse
Updated on 11-Feb-2020 11:15:38

2K+ Views

C++98 was the first edition of the C++ standard. It had defined all the basic language constructs, the STL, and the standard library.C++03 was the next revision to this standard. This was majorly a considered a bugfix for the standard as it corrected 92 core language defect reports, 125 library defect reports, and included only one new language feature: value initialization.C++0x was the name of the work in progress that was expected to complete by 2008-09 but finally completed in 2011.C++11 was the modern C++ standard published in 2011. This brought many major extensions and improvements to the existing language. ... Read More

What is Rule of Five in C++11?

Chandu yadav
Updated on 24-Jun-2020 05:50:29

756 Views

The rule of five is applied in C++ for resource management. Resource management frees the client from having to worry about the lifetime of the managed object, potentially eliminating memory leaks and other problems in the C++ code. But this management comes at a cost. The Rule of The Big Five states that if you have to write one of the following functions then you have to have a policy for all of them. If we have an Object Foo then we can have a FooManager that handles the resource Foo. When implementing FooManager, you'll likely all need the following ... Read More

Rule of Three vs Rule of Five in C++?

George John
Updated on 24-Jun-2020 05:51:52

656 Views

The Rule of three is a rule of thumb when using C++. This is kind of a good practice rule that says that If your class needs any ofa copy constructor, an assignment operator, or a destructor, defined explicitly, then it is likely to need all three of them.Why is this? Its because, if your class requires any of the above, it is managing dynamically allocated resources and would likely be needing the other to successfully achieve that. For example, if you require an assignment operator, you would be creating copies of objects currently being copied by reference, hence allocating ... Read More

Why aren't variable-length arrays part of the C++ standard?

Abhinaya
Updated on 24-Jun-2020 05:52:35

510 Views

Having to create a potential large array on the stack, which usually has only little space available, isn't good. If you know the size beforehand, you can use a static array. And if you don't know the size beforehand, you will write unsafe code. Variable-length arrays can not be included natively in C++ because they'll require huge changes in the type system.An alternative to Variable-length arrays in C++ is provided in the C++ STL, the vector. You can use it like −Example#include #include using namespace std; int main() {    vector vec;    vec.push_back(1);    vec.push_back(2);    vec.push_back(3);   ... Read More

How do I declare a two-dimensional array in C++ using new?

Chandu yadav
Updated on 11-Feb-2020 11:07:10

2K+ Views

A dynamic 2D array is basically an array of pointers to arrays. So you first need to initialize the array of pointers to pointers and then initialize each 1d array in a loop.example#include using namespace std; int main() {    int rows = 3, cols = 4;    int** arr = new int*[rows];    for(int i = 0; i < rows; ++i)    arr[i] = new int[cols];    return 0; } This will create an 2D array of size 3x4. Be vary of clearing the memory in such cases as you'll need to delete the memory in the ... Read More

Difference between #include and #include "filename" in C/C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:22

3K+ Views

The difference between the two forms is in the location where the preprocessor searches for the file to be included.#include The preprocessor searches in an implementation-dependent manner, it searches directories pre-designated by the compiler. This method is usually used to include standard library header files.#include "filename"The preprocessor searches in the same directory as the file containing the directive. If this fails, then it starts behaving like the #include form. This method is usually used to include your own header files.

Difference between const int*, const int * const, and int const * in C/C++?

George John
Updated on 11-Feb-2020 11:04:26

4K+ Views

The above symbols mean the following −int* - Pointer to int. This one is pretty obvious. int const * - Pointer to const int. int * const - Const pointer to int int const * const - Const pointer to const intAlso note that −const int * And int const * are the same. const int * const And int const * const are the same.If you ever face confusion in reading such symbols, remember the Spiral rule: Start from the name of the variable and move clockwise to the next pointer or type. Repeat until expression ends.

Tokenize a string in C++?

Ramu Prasad
Updated on 11-Feb-2020 11:03:12

380 Views

First way is using a stringstream to read words seperated by spaces. This is a little limited but does the task fairly well if you provide the proper checks. example#include #include #include using namespace std; int main() {    string str("Hello from the dark side");    string tmp; // A string to store the word on each iteration.    stringstream str_strm(str);    vector words; // Create vector to hold our words    while (str_strm >> tmp) {       // Provide proper checks here for tmp like if empty       // ... Read More

How to read and parse CSV files in C++?

Nitya Raut
Updated on 11-Feb-2020 10:54:53

3K+ Views

You should really be using a library to parsing CSV files in C++ as there are many cases that you can miss if you read files on your own. The boost library for C++ provides a really nice set of tools for reading CSV files. For example, example#include vector parseCSVLine(string line){    using namespace boost;    std::vector vec;    // Tokenizes the input string    tokenizer tk(line, escaped_list_separator    ('\', ', ', '\"'));    for (auto i = tk.begin();  i!=tk.end();  ++i)    vec.push_back(*i);    return vec; } int main() {    std::string line = "hello, from, ... Read More

How do you set, clear, and toggle a bit in C/C++?

Chandu yadav
Updated on 11-Feb-2020 10:52:13

9K+ Views

You can set clear and toggle bits using bitwise operators in C, C++, Python, and all other programming languages that support these operations. You also need to use the bitshift operator to get the bit to the right place.Setting a bitTo set a bit, we'll need to use the bitwise OR operator −Example#include using namespace std; int main() {    int i = 0, n;        // Enter bit to be set:    cin >> n;    i |= (1 > n;    i ^= (1

Advertisements