Found 27104 Articles for Server Side Programming

Tokenize a string in C++?

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

202 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

7K+ 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

How to compare float and double in C++?

Sravani S
Updated on 24-Jun-2020 05:41:44

571 Views

Comparing floats and double variables depends on what your end goal is. If you want a runnable function without going too much in details and won't have a problem in some inaccurate calculations you can use the following function −Example#include using namespace std; // Define the error that you can tolerate #define EPSILON 0.000001 bool areSame(double a, double b) {    return fabs(a - b) < EPSILON; } int main() {    double a = 1.005;    double b = 1.006;    cout

How to access a local variable from a different function using C++ pointers?

George John
Updated on 24-Jun-2020 05:42:36

1K+ Views

You can't access a local variable once it goes out of scope. This is what it means to be a local variable. Though, Let us look at an example where you MIGHT be able to access a local variable's memory outside its scope.Example#include int* foo() {    int x = 3;    return &x; } int main() {    int* address = foo();    cout

Explain C++ Singleton design pattern.

Jennifer Nicholas
Updated on 24-Jun-2020 05:42:05

15K+ Views

Singleton design pattern is a software design principle that is used to restrict the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. For example, if you are using a logger, that writes logs to a file, you can use a singleton class to create such a logger. You can create a singleton class using the following code −Example#include using namespace std; class Singleton {    static Singleton *instance;    int data;      // Private constructor so that no objects can be created. ... Read More

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used in C++?

V Jyothi
Updated on 23-Jun-2020 13:57:11

3K+ Views

const_castcan be used to remove or add const to a variable. This can be useful if it is necessary to add/remove constness from a variable.static_castThis is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc.dynamic_castThis cast is used for handling polymorphism. You only need to use it when you're casting to a derived class. This is exclusively to be used in inheritence when you cast from base class to derived class.reinterpret_castThis is ... Read More

Where can I find the current C or C++ standard documents?

Nishtha Thakur
Updated on 23-Jun-2020 13:59:38

74 Views

You can find the current C standard documents on ANSI web store. https://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+9899-2012You can find the current C++ standard documents on the ISO C++ website for buying −  https://www.iso.org/standard/68564.htmlThe working draft of the ISO C++ standard is also available on  https://isocpp.org/std/the-standard

What is a smart pointer and when should I use it in C++?

George John
Updated on 30-Jul-2019 22:30:21

293 Views

A smart pointer is a class that wraps a 'raw' (or 'bare') C++ pointer. It is used to manage resources the pointer points to. For example, if the reference to that memory location is lost. It kind of acts like a garbage collector. There are multiple smart pointer types.You should almost always use a smart pointer. This is because the main pain point of using pointers is manual memory management and memory leaks. The smart pointer tries to get rid of both of these. If you don't want to do either of these in practice, you should use a smart ... Read More

When can I use a forward declaration C/C++?

Priya Pallavi
Updated on 11-Feb-2020 10:43:13

165 Views

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.

Advertisements