C++ Operators
Operators are used to perform operations on variables and values.
In the example below, we use the +
operator to add
together two values:
ऑपरेटरों का उपयोग variables और उसके
value पर operation करने
के लिए किया जाता है।
नीचे दिए गए उदाहरण में, हम दो value को एक
साथ जोड़ने के लिए + operator का
उपयोग करते हैं:
Example
int x = 100 + 50;
Although the +
operator is often used to
add together two values, like in the example above, it can also be used to add
together a variable and a value, or a variable and another variable:
हालाँकि + ऑपरेटर का उपयोग अक्सर दो मानों को एक साथ जोड़ने
के लिए किया जाता है, जैसा कि ऊपर दिए गए उदाहरण में है, इसका
उपयोग एक variable और एक value, या एक variable और
अन्य variable को एक
साथ जोड़ने के लिए भी किया जा सकता है:
Example
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
C++ divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise
operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical
operations.
Arithmetic operator का
उपयोग सामान्य mathematical operation करने के
लिए किया जाता है।
Operator |
Name |
Description |
Example |
+ |
Addition |
Adds together two values |
x + y |
- |
Subtraction |
Subtracts one value from another |
x - y |
* |
Multiplication |
Multiplies two values |
x * y |
/ |
Division |
Divides one value by another |
x / y |
% |
Modulus |
Returns the division remainder |
x % y |
++ |
Increment |
Increases the value of a variable by 1 |
++x |
-- |
Decrement |
Decreases the value of a variable by 1 |
--x |
C++ Assignment Operators
Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator
(=
) to
assign the value 10 to
a variable called x:
Example
int x = 10;
The addition
assignment operator (+=
) adds
a value to a variable:
Example
int x = 10;
x += 5;
A list of all assignment operators:
Operator |
Example |
Same As |
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 3 |
x = x * 3 |
/= |
x /= 3 |
x = x / 3 |
%= |
x %= 3 |
x = x % 3 |
&= |
x &= 3 |
x = x & 3 |
|= |
x |= 3 |
x = x | 3 |
^= |
x ^= 3 |
x = x ^ 3 |
>>= |
x >>= 3 |
x = x >> 3 |
<<= |
x <<= 3 |
x = x << 3 |
C++ Strings
C++ Strings
Strings are used for storing text.
A string
variable contains a
collection of characters surrounded by double quotes:
Strings का
उपयोग text को
स्टोर करने के लिए किया जाता है।
एक string variable, double quotes sign से
घिरे characters का एक
संग्रह होता है:
Example
Create a variable of type string
and
assign it a value:
string
greeting = "Hello";
To use strings, you must include an additional header file in the
source code, the <string>
library:
String का
उपयोग करने के लिए, आपको source code में, एक
अतिरिक्त हेडर फ़ाइल <string> लाइब्रेरी
शामिल करनी होगी:
Example
//
Include the string library
#include <string>
// Create a string variable
string greeting = "Hello";
C++ String Concatenation
String Concatenation
The +
operator can be used
between strings to add them together to make a new string. This is called concatenation:
नई string बनाने
के लिए उन्हें एक साथ जोड़ने के लिए string के बीच + operator का
उपयोग किया जा सकता है। इसे concatenation कहा जाता है:
Example
string
firstName = "John
";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
In the example above, we added a space after firstName to create a
space between John and Doe on output. However, you could also add a space with
quotes (" "
or ' '
):
उपरोक्त उदाहरण में, हमने output मे John और Doe के बीच एक स्थान बनाने के लिए firstName के बाद
एक space जोड़ा।
हालाँकि, आप quotes
(" " या '') के साथ एक space भी जोड़ सकते हैं:
Example
string
firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName;
Append
A string in C++ is actually an object, which contain functions
that can perform certain operations on strings. For example, you can also
concatenate strings with the append()
function:
C++ में एक string वास्तव में एक object है, जिसमें
ऐसे functions होते
हैं जो string पर कुछ
operation कर
सकते हैं। उदाहरण के लिए, आप append() function के साथ
strings को भी
जोड़ सकते हैं:
Example
string
firstName = "John
";
string lastName = "Doe";
string fullName = firstName.append(lastName);
cout << fullName;
C++ Numbers and Strings
Adding Numbers and Strings
WARNING!
C++ uses the +
operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
चेतावनी!
C++ addition और concatenation दोनों
के लिए + ऑपरेटर का उपयोग करता है।
नंबर जोड़े जाते हैं. Strings concatenate होते हैं.
If you add two numbers, the result will be a number:
Example
int x = 10;
int y = 20;
int z = x + y; // z will be 30 (an integer)
If you add two strings, the result will be a string concatenation:
Example
string
x = "10";
string y = "20";
string z = x + y; // z will be 1020 (a string)
If you try to add a number to a string, an error occurs:
Example
string
x = "10";
int y = 20;
string z = x + y;
C++ String Length
String Length
To get the length of a string, use the length()
function:
String के length को पाने के लिये length() function का उपयोग करते है।
Example
string
txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();
Tip: You might see some C++ programs that
use the size()
function to get the length of a
string. This is just an alias of length()
. It is
completely up to you if you want to use length()
or size()
:
Example
string
txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.size();
C++ Access Strings
Access Strings
You can access the characters in a string by referring to its
index number inside square brackets []
.
This example prints the first character in myString:
आप square brackets [] के अंदर इसकी index number को देकर string के characters को प्राप्त
कर सकते हैं।
यह उदाहरण myString के पहला
अक्षर प्रिंट करता है:
Example
string myString
= "Hello";
cout << myString[0];
// Outputs H
Note: String indexes start with 0: [0] is
the first character. [1] is the second character, etc.
String index 0
से शुरू होते हैं: [0] पहला
अक्षर है। [1] दूसरा character है, आदि।
This example prints the second character in myString:
Example
string myString
= "Hello";
cout << myString[1];
// Outputs e
Change String Characters
To change the value of a specific character in a string, refer to
the index number, and use single quotes:
किसी string में किसी विशिष्ट character का value बदलने के लिए, index number देखें
और single quotes sign का
उपयोग करें:
Example
string myString
= "Hello";
myString[0] = 'J';
cout << myString;
// Outputs Jello instead of Hello
C++ Special Characters
Strings - Special Characters
Because strings must be written within quotes, C++ will
misunderstand this string, and generate an error:
चुकि string को quotes के अंदर लिखना जाता है, इश्लिये C++ इस string को नहि समझ पायेगा
और error देगा
string
txt = "We are the
so-called "Vikings" from the north.";
The solution to avoid this problem, is to use the backslash escape character.
The backslash (\
) escape character turns
special characters into string characters:
इस problem का सरल उपाय है, backslash
escape character का उप्योग
किय जाये।
Backslash (\) escape character, special character को string
character मे बदल
देता है।
Escape character |
Result |
Description |
\' |
' |
Single quote |
\" |
" |
Double quote |
\\ |
\ |
Backslash |
The sequence \"
inserts a double quote in a string:
Example
string
txt = "We are the
so-called \"Vikings\" from the north.";
The sequence \'
inserts a single
quote in a string:
Example
string
txt = "It\'s
alright.";
The sequence \\
inserts a single
backslash in a string:
Example
string
txt = "The character
\\ is called backslash.";
Other popular escape characters in C++ are:
C++ मे अन्य escape characters निम्न्लिखित है:-
Escape Character |
Result |
Try it |
\n |
New Line |
|
\t |
Tab |
C++ User Input Strings
User Input Strings
It is possible to use the extraction operator >>
on cin
to
store a string entered by a user:
किसी user द्वारा enter की गई स्ट्रिंग को store करने के लिए cin के साथ extraction operator >>
का उपयोग किया जाता है:
Example
string
firstName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;
// Type your first name: John
// Your name is: John
However, cin
considers a space
(whitespace, tabs, etc) as a terminating character, which means that it can
only display a single word (even if you type many words):
हालाँकि, cin एक space (white space, tab, आदि) को
एक terminating character के रूप
में मानता है, जिसका अर्थ है कि यह केवल एक word store कर
सकता है (भले ही आप कई शब्द लिखे करें):
Example
string
fullName;
cout << "Type your full name: ";
cin >> fullName;
cout << "Your name is: " << fullName;
// Type your full name: John Doe
// Your name is: John
From the example above, you would expect the program to print
"John Doe", but it only prints "John".
That's why, when working with strings, we often use the getline()
function
to read a line of text. It takes cin
as the first
parameter, and the string variable as second:
उपरोक्त उदाहरण से, आप
उम्मीद करेंगे कि प्रोग्राम "John Doe" प्रिंट करेगा, लेकिन
यह केवल "John"
प्रिंट करता है।
इसीलिए, string के साथ काम करते समय, हम
अक्सर text की एक
पंक्ति को पढ़ने के लिए getline() फ़ंक्शन का उपयोग करते हैं। यह cin को पहले
parameter के रूप
में लेता है, और string variable को
दूसरे parameter के रूप
में लेता है:
Example
string
fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
// Type your full name: John Doe
// Your name is: John Doe
C++ String Namespace
Omitting Namespace
You might see some C++ programs that runs without the standard
namespace library. The using namespace std
line
can be omitted and replaced with the std
keyword,
followed by the ::
operator for string
(and cout
)
objects:
आप कुछ C++ प्रोग्राम देख सकते हैं जो standard namespace library के
बिना चलते हैं। namespace std लाइन
का उपयोग छोड़ा जा सकता है और std keyword के साथ
replace किया
जा सकता है, इसके बाद string और cout ऑब्जेक्ट के बीच मे :: operator का प्रयोग किया जाता है:
Example
#include
<iostream>
#include <string>
int main() {
std::string greeting = "Hello";
std::cout << greeting;
return 0;
}
C++ Math
C++ has many functions that allow you to perform mathematical
tasks on numbers.
C++ में कई फ़ंक्शन हैं जो आपको संख्याओं पर mathematical कार्य
करने की अनुमति देते हैं।
Max and min
The max(
x,
y)
function
can be used to find the highest value of x and y:
Max(x,y) function का use, x और y मे से कौन बरा है, उसको बताने के लिये किया जाता है
Example
cout
<< max(5, 10);
And the min(
x,
y)
function
can be used to find the lowest value of x and y:
और min(x,y) function का उपयोग, x और y मे से कौन छोटा है, उसे बतने के लिये होता है।
Example
cout
<< min(5, 10);
C++ <cmath> Header
Other functions, such as sqrt
(square
root), round
(rounds a number) and log
(natural
logarithm), can be found in the <cmath>
header
file:
अन्य फ़ंक्शन, जैसे कि
sqrt (square root), round (round a number) और log (natural logarith),
<cmath> हेडर फ़ाइल में पाए जा सकते हैं:
Example
//
Include the cmath library
#include <cmath>
cout << sqrt(64);
cout << round(2.6);
cout << log(2);
Other Math Functions
A list of other popular Math functions (from the <cmath>
library)
can be found in the table below:
Function |
Description |
abs(x) |
Returns the absolute value of x |
acos(x) |
Returns the arccosine of x |
asin(x) |
Returns the arcsine of x |
atan(x) |
Returns the arctangent of x |
cbrt(x) |
Returns the cube root of x |
ceil(x) |
Returns the value of x rounded up to its
nearest integer |
cos(x) |
Returns the cosine of x |
cosh(x) |
Returns the hyperbolic cosine of x |
exp(x) |
Returns the value of Ex |
expm1(x) |
Returns ex -1 |
fabs(x) |
Returns the absolute value of a floating
x |
fdim(x, y) |
Returns the positive difference between x
and y |
floor(x) |
Returns the value of x rounded down to
its nearest integer |
hypot(x, y) |
Returns sqrt(x2 +y2) without intermediate
overflow or underflow |
fma(x, y, z) |
Returns x*y+z without losing precision |
fmax(x, y) |
Returns the highest value of a floating x
and y |
fmin(x, y) |
Returns the lowest value of a floating x
and y |
fmod(x, y) |
Returns the floating point remainder of
x/y |
pow(x, y) |
Returns the value of x to the power of y |
sin(x) |
Returns the sine of x (x is in radians) |
sinh(x) |
Returns the hyperbolic sine of a double
value |
tan(x) |
Returns the tangent of an angle |
tanh(x) |
Returns the hyperbolic tangent of a
double value |
No comments:
Post a Comment