Programming Articles

Page 2338 of 2547

How to replace backward "" slash from a string

Malhar Lathkar
Malhar Lathkar
Updated on 23-Jun-2020 729 Views

In Python it does give the desired result>>> var  = "aaa\bbb\ccc and ddd\eee" >>> var.split('') ['aaa', 'bbb', 'ccc and ddd', 'eee']

Read More

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

Nishtha Thakur
Nishtha Thakur
Updated on 23-Jun-2020 195 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

Read More

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

V Jyothi
V Jyothi
Updated on 23-Jun-2020 4K+ 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

What is the copy-and-swap idiom in C++?

Ankith Reddy
Ankith Reddy
Updated on 23-Jun-2020 383 Views

The assignment consists of 2 steps, tearing an object's old state and building a new state for it. The destructor is used for the first step and a copy constructor does the second step. Implementing both of these is straightforward. But when overloading the assignment operator, it can become quite difficult to implement. The copy and swap idiom is a solution for the same.This idiom uses the copy-constructor to build a local copy of the data. It then swaps the old data with the new data using the swap function. The temporary copy is then destructed using the destructor. We ...

Read More

Why is it faster to process a sorted array than an unsorted array in C++?

Anvi Jain
Anvi Jain
Updated on 23-Jun-2020 370 Views

In C++, it is faster to process a sorted array than an unsorted array because of branch prediction. In computer architecture, a branch prediction determines whether a conditional branch (jump) in the instruction flow of a program is likely to be taken or not.Let’s take an example −if(arr[i] > 50) {    Do some operation B } else {    Do some operation A }If we run this code for 100 elements in unsorted and sorted order below things will be happened −For sorted array −1, 2, 3, 4, 5, …… 50, 51………100 A, A, A, A, A A, B ...

Read More

What is The Rule of Three with reference to C++?

Govinda Sai
Govinda Sai
Updated on 23-Jun-2020 221 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

What are undefined reference/unresolved external symbol errors in C++?

Daniol Thomas
Daniol Thomas
Updated on 23-Jun-2020 1K+ Views

As the name suggests, a symbol you declared was not defined by you. This may occur due to many cases. Let's have a look at three of them −You forgot to define the declared name. For example, you declared a function in a file and used it somewhere. But you did not provide its definition. Code −#include void foo(); int main() {    foo(); // Declared but not defined }You defined it but did not use the qualified name. Say you created a class with a method and defined that method but forgot using scope resolution to link that function ...

Read More

Why we can't use arrow operator in gets and puts?

Pythonista
Pythonista
Updated on 22-Jun-2020 240 Views

You can't read user input in an un-initialized pointer. Instead, have a variable of the struct data type and assign its address to pointer before accessing its inner elements by → operatorexample#include struct example{ char name[20]; }; main(){ struct example *ptr; struct example e; puts("enter name"); gets(e.name); ptr=&e; puts(ptr->name); }OutputTypical result of above codeenter name Disha You entered Disha

Read More

Why we should use whole string in Java regular expression

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Jun-2020 1K+ Views

In Java regex matches() matches the input string against the whole string as it add a ^ and $ at the end of the input string.so it will not match the substring. So for matching substring, you should use find().Exampleimport java.util.regex.*; class PatternMatchingExample {    public static void main(String args[]) {       String content = "aabbcc";       String string = "aa";       Pattern p = Pattern.compile(string);       Matcher m = p.matcher(content);       System.out.println(" 'aa' Match:"+ m.matches());       System.out.println(" 'aa' Match:"+ m.find());    } }Output'aa' Match:false 'aa' Match:true

Read More

How to extract a group from a Java String that contains a Regex pattern

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Jun-2020 390 Views

How to extract a group from a Java String that contains a Regex patternimport java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTest {    public static void main(String[] args) {       Pattern pattern = Pattern.compile("fun");       Matcher matcher = pattern.matcher("Java is fun");       // using Matcher find(), group(), start() and end() methods       while (matcher.find()) {          System.out.println("Found the text "" + matcher.group()             + "" starting at " + matcher.start()             + " index and ending at index ...

Read More
Showing 23371–23380 of 25,466 articles
Advertisements