C++ Articles

Page 541 of 597

Tokenize a string in C++?

Ramu Prasad
Ramu Prasad
Updated on 11-Feb-2020 459 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
Nitya Raut
Updated on 11-Feb-2020 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) ...

Read More

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

Chandu yadav
Chandu yadav
Updated on 11-Feb-2020 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

Read More

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

Priya Pallavi
Priya Pallavi
Updated on 11-Feb-2020 317 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.

Read More

Why is not sizeof for a struct equal to the sum of sizeof of each member in C/C++?

Chandu yadav
Chandu yadav
Updated on 11-Feb-2020 340 Views

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

Read More

How can I profile C++ code running in Linux?

Vrundesha Joshi
Vrundesha Joshi
Updated on 11-Feb-2020 1K+ Views

There are many great profiling tools for profiling C++ programs on Linux. The most widely used tool is Valgrind. It is a programming tool for memory debugging, memory leak detection, and profiling. You can use valgrind by passing the binary to it and setting the tool to callgrind. First generate the binary by compiling the program −$ g++ -o hello.cpp hello Now use valgrind to profile it: $ valgrind --tool=callgrind ./helloThis will generate a file called callgrind.out.x. You can read this file using a tool called kcachegrind.If you're using gcc, you can use the inbuilt profiling tool, gprof. You can ...

Read More

The most elegant way to iterate the words of a C/C++ string

Arjun Thakur
Arjun Thakur
Updated on 11-Feb-2020 2K+ Views

There is no one elegant way to iterate the words of a C/C++ string. The most readable way could be termed as the most elegant for some while the most performant for others. I've listed 2 methods that you can use to achieve this. 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;         ...

Read More

What are the rules about using an underscore in a C++ identifier?

Arjun Thakur
Arjun Thakur
Updated on 11-Feb-2020 2K+ Views

From MSDN docs −Use of two sequential underscore characters ( __ ) at the beginning of an identifier, or a single leading underscore followed by a capital letter, is reserved for C++ implementations in all scopes. You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers.So you should avoid using names like −__foo, __FOO, _FOOAnd names like the following should not be used in the global namespace −_foo, _barOther than this, there are some more prefixes like LC_, SIG_, and suffixes like _t ...

Read More

Can a local variable's memory be accessed outside its scope in C/C++?

Rishi Rathor
Rishi Rathor
Updated on 11-Feb-2020 255 Views

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

Read More

Why can C++ templates only be implemented in the header file?

Ramu Prasad
Ramu Prasad
Updated on 11-Feb-2020 989 Views

When you instantiate a template in C++, the compiler creates a new class. This class has all the places where you placed the template arguments replaced with the actual argument you pass to it when using it. For example −template class MyClass {    T foo;    T myMethod(T arg1, T arg2) {       // Impl    } };And somewhere in your program use this class, MyClass x;The compiler creates a new class upon encountering this for every type argument you pass it. For example, if you created 3 objects with different template arguments you'll get 3 classes, ...

Read More
Showing 5401–5410 of 5,962 articles
« Prev 1 539 540 541 542 543 597 Next »
Advertisements