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
Scott Meyers in Effective C++ says −If a class has any virtual function, it should have a virtual destructor, and that classes not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors.So you should declare destructors virtual in polymorphic base classes. This is because if you create an object of base class using a derived constructor −Base *b = new Derived(); // use b delete b;If Base's destructor is not virtual then delete b has undefined behavior in this case. The call to the destructor will be resolved like any non-virtual code. ... Read More
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.
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
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
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
Forward declaration lets the code following the declaration know that there is are classes with the name Person. This satisfies the compiler when it sees these names used. Later the linker will find the definition of the classes. exampleClass Person; void myFunc(Person p1) { // ... } Class Person { // Class definition here };So in this case when compiler encounters myFunc, it'll know that its going to encounter this class somewhere down in the code. This can be used in cases where code using the class is placed/included before the code containing the class definition.
Arrays and pointers work quite similarly in C/C++. But there are some subtle differences. For example, the sizeof operator works quite differently on the two. When you convert an array in a pointer,Example#include int main() { const int a[] = { 2, 3, 5, 7, 11 }; const int* p = a; std::cout
The difference between sizeof for a struct and the sum of sizeof of each member of that struct is due to byte padding and alignment. Every data type in C/C++ has a alignment requirement. A processor will have processing word length of its architecture. On a 32 bit machine, the processing word size will be 4 bytes or 32 bits. For example, If you have the struct −Example#include using namespace std; struct X { char b[3]; int c; }; int main() { char b[3]; int c; int total = sizeof(b) + sizeof(c); cout
A SwingWorker class enables us to perform an asynchronous task in a worker thread (such as a long-running task) then update Swing components from the Event Dispatch Thread (EDT) based on the task results. It was introduced in Java 1.6 Version.SwingWorker classThe java.swing.SwingWorker class is a task worker, which performs time-consuming tasks in the background.A SwingWorker instance interacts with 3 threads, Current thread, the Worker thread, and the Event Dispatch thread(EDT).The Current thread calls the execute() method to kick off the task to the background and returns immediately.The Worker thread executes our own version of the doInBackground() method continuously in the background.The Event Dispatch Thread (EDT) wakes up from time to ... Read More
 Data Structure
 Networking
 RDBMS
 Operating System
 Java
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP