Found 7346 Articles for C++

How to convert an enum type variable to a string in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

410 Views

Here we will see how to convert some enum type data to a string in C++. There is no such direct function to do so. But we can create our own function to convert enum to string.We shall create a function that takes an enum value as an argument, and we manually return the enum names as a string from that function.Example Code Live Demo#include using namespace std; enum Animal {Tiger, Elephant, Bat, Dog, Cat, Mouse}; string enum_to_string(Animal type) {    switch(type) {       case Tiger:          return "Tiger";       case ... Read More

How to convert a std::string to const char* or char* in C++?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

In this section, we will see how to convert C++ string (std::string) to const char* or char*. These formats are C style strings. We have a function called c_str(). This will help us to do the task. It returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.Following is the declaration for std::string::c_str.const char* c_str() const;This function returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object. If an exception is thrown, ... Read More

Differences between C++ string == and compare()?

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

3K+ Views

In C++ we can compare two strings using compare() function and the == operator. Then the question is why there are two different methods? Is there any difference or not?There are some basic differences between compare() and == operator. In C++ the == operator is overloaded for the string to check whether both strings are same or not. If they are the same this will return 1, otherwise 0. So it is like Boolean type function.The compare() function returns two different things. If both are equal, it will return 0, If the mismatch is found for character s and t, ... Read More

Difference between string and char[] types in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

709 Views

In this section we will see what are the differences between string and the char[] in C++. The char[] is basically an array of characters. So there are some properties of this array. These properties are listed below.If the char[] is allocated in the stack section then it will always occupy 256 bytes of space. It will not depend on the size of the text.If we use malloc() or calloc() to dynamically allocate space for it into the heap section then we are responsible for releasing the memory after using this, and we always have the overhead of a heap ... Read More

C++ Program to Implement String Matching Using Vectors

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

486 Views

This is another string matching method. In this approach, we are searching for a substring using vectors.In C++ we can create vectors easily using the standard library. We are taking the main string and the string that will be searched as a vector, then searching it into the main string. When one match is found the function returns the address, and removes it from the main string. So in the next iteration, it starts from location 0 and searches again.For multiple occurrences, we are using loops and repeatedly searching for the match, and return the position.Input: Main String: “ABAAABCDBBABCDDEBCABC”, Pattern ... Read More

Wrapping C/C++ for Python using SWIG

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

1K+ Views

There are multiple ways to wrap your existing C or C++ functionality in Python. In this section, we are going to see how we can wrap C/C++ functionality with SWIG. Below are other options to wrap c/c++ functionality in python.Manual wrappingUsing pyrex to wrap C code.CtypesSIPBoost PythonSWIG (Simple Wrapper Interface Generator) is capable of wrapping C code with numerous other languages including Perl, Python, PHP, Ruby, Tcl, C#, Common Lisp (CLISP, Allegro, CL, UFFI, CFFI), Java, Modula-3 and OCAML. Swig also supports multiple interpreted and compiled Scheme implementations (like Guile, MzScheme, Chicken) are supported.But we will discuss its implementation with ... Read More

negate function in C++ STL

Nitya Raut
Updated on 30-Jul-2019 22:30:25

794 Views

Negate function is used to negate the given values such that to change the sign of the values. It changes the negative values to positive and vice-versa.Function prototype:function transform(a_begin, a_end, a1_begin, negate()):    a_begin = lower bound of the array.    a_end = upper bound of the array.    a1_end = Lower bound of the second modified array.    negate() = to negate the values of the array.Example Code#include #include #include using namespace std; int main() {    int a[] = { 4,6,7, -10, -20, -30 };    transform(a, a + 6, a, negate());    for (int i = 0; i < 6; i++)       cout

multiset insert() function in C++ STL

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

150 Views

The multiset insert() function in C++ STL which insert elements in the multiset container from a position to another position from one multiset to a different multiset.List of functions used:ms.size() = Returns the size of multiset. ms.insert() = It is used to insert elements to the multiset.Example Code#include #include #include #include using namespace std; int main() {    multiset ms;    multiset::iterator it, it1;    int c, i;    while (1) {       cout

lldiv() function in C++ STL

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

86 Views

lldiv() function in C++ STL gives the result of quotient and remainder of the division of the two numbers.AlgorithmBegin Take two long type numbers as input. Call function lldiv(). Print the quotient and remainder. End.Example Code#include #include using namespace std; int main() {    long long q = 500LL;    long long r = 25LL;    lldiv_t res = lldiv(q, r);    cout

iswalpha() function in C++ STL

Nitya Raut
Updated on 30-Jul-2019 22:30:25

83 Views

iswalpha() function in C++ STL is used to check whether the given wide character is an alphabet or not.AlgorithmBegin Initializes the strings. Call function iswalpha(str) to check whether it contains alphabet or not. If it contains alphabet, then value will be returned otherwise zero will be returned. EndExample Code#include #include #include #include using namespace std; int main() {    setlocale(LC_ALL, "ab1234");    wchar_t s[] = L"a%$^^&)";    bool flag = 0;    for (int i=0; i

Advertisements