C++ का std::string
In this page:
#include <string>
std::string variable_name;
std::string variable_name = "text";
std::string क्या है?
<string> header से std::string, C++ का modern string type है जो अपनी memory allocation और resizing automatically handle करता है, एक raw C-style char array के उलट जिसका एक fixed size है जिसे आपको खुद manage करना होता है।
यह automatic memory management buffer overflows और manual sizing के आस-पास bugs की एक पूरी category खत्म कर देता है।
उदाहरण: What is std::string?
#include <iostream>
#include <string>
int main() {
std::string name = "Alex"; // grows/shrinks automatically, no fixed buffer size
std::cout << name << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declare और Initialize करना
एक std::string empty बनाया जा सकता है (string s;), सीधे एक literal से initialize किया जा सकता है (string s = "hello";), या एक constructor से बनाया जा सकता है जो एक character को एक set संख्या में दोहराता है (string s(5, '*'); "*****" बनाता है), आप जो बना रहे हैं उसके हिसाब से flexibility देते हुए।
उदाहरण: Declaring and Initializing
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string a;
std::string b = "hello";
std::string c(5, 'x');
// Print to the console with cout
std::cout << a << "|" << b << "|" << c << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
String Concatenation
+ और += operators आपको strings को dynamically साथ जोड़ने देते हैं, जैसे string full = firstName + " " + lastName;, ज़रूरत पड़ने पर resulting string की memory automatically बढ़ाते हुए — जिसके लिए C-style strings के साथ careful manual buffer management चाहिए होगी।
उदाहरण: String Concatenation
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string firstName = "Alex";
std::string lastName = "Smith";
std::string full = firstName + " " + lastName;
// Print to the console with cout
std::cout << full << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
cin से Strings पढ़ना
C-style strings की तरह ही, cin >> पहले whitespace character पर पढ़ना रोक देता है, इसलिए यह एक std::string में सिर्फ एक single word capture कर सकता है।
Global getline(cin, myString) function इसकी बजाय एक पूरी line पढ़ता है, spaces समेत, जो लगभग हमेशा वही है जो आप एक user से एक पूरे response के लिए prompt करते समय actually चाहते हैं।
उदाहरण: Reading Strings with cin
#include <iostream>
#include <string>
int main() {
std::string name = "Alex"; // cin >> would stop at the first space
std::cout << name << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Characters Access करना
Individual characters [] से access किए जा सकते हैं same तरह जैसे एक array के लिए, जैसे name[0], लेकिन [] check नहीं करता कि index actually valid है या नहीं — .at(index) bounds checking के साथ same access perform करता है, अगर index range से बाहर हो तो चुपचाप invalid memory पढ़ने की बजाय एक exception throw करते हुए।
उदाहरण: Accessing Characters
#include <iostream>
#include <string>
int main() {
std::string name = "Alex";
std::cout << name[0] << std::endl; // [] performs no bounds checking
return 0;
}
Login to try C/C++/Java/PHP code in the editor
cin >> sसे एक पूरा sentence पढ़ना, जो पहले whitespace पर रुकता है; पूरी lines के लिएgetline(cin, s)इस्तेमाल करें।#include <string>भूल जाना औरstd::stringavailable होने की उम्मीद करना, जो कुछ compilers पर compile होने में fail हो सकता है।- दो string literals के साथ
"Hello" + " World"लिखना, जो compile नहीं होता क्योंकि कोई भी sidestd::stringनहीं है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings