C++ की STL String
In this page:
std::string string_name = "text";
string_name.append("more");
string_name.insert(position, "text");
string_name.erase(position, count);
String Creation और Basic Methods
std::string text के लिए एक safe, dynamically-resizing container है जो अपनी memory खुद automatically manage करता है, एक raw char array के उलट जिसका एक fixed size है और कोई bounds checking नहीं।
आप एक literal, दूसरी string, या repeated characters से एक बनाते हैं, और class पीछे से allocation handle करती है।
उदाहरण: String Creation and Basic Methods
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string name = "Alex";
// Print to the console with cout
std::cout << name.length() << 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 Modification
append(), insert(), erase(), और operator+= जैसे methods आपको buffer size manually manage किए बिना एक string के contents बढ़ने या सिकुड़ने देते हैं।
क्योंकि std::string खुद resize होता है, ये operations एक fixed-size C-style char array के अंत से आगे लिखने से कहीं ज़्यादा safe हैं।
उदाहरण: String Modification
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string text = "Hello";
text.append(" World");
text += "!";
// Print to the console with cout
std::cout << text << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Substrings ढूंढना
find() एक character या substring के लिए scan करता है और पहले match का zero-based index return करता है, या अगर कुछ न मिले तो special value std::string::npos -- एक valid index मान लेने की बजाय हमेशा npos के against check करें। rfind() end से same search perform करता है।
उदाहरण: Finding Substrings
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string text = "Hello, World!";
// Declare pos and set it to text.find("World")
size_t pos = text.find("World");
if (pos != std::string::npos) std::cout << "Found at " << pos << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Substrings और Comparisons
substr(pos, len) एक दी गई position से शुरू होकर एक नई string extract करता है, जो dates या filenames जैसे fixed-format text parse करने के लिए handy है।
Comparison operators (==, <, >) strings को lexicographically compare करते हैं, character by character, उसी तरह जैसे एक dictionary में words sort होते हैं।
उदाहरण: Substrings and Comparisons
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string date = "2024-01-15";
std::string year = date.substr(0, 4);
// Print to the console with cout
std::cout << year << std::endl;
// Print to the console with cout
std::cout << (year == "2024") << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Numeric Conversions
std::stoi, std::stod, और similar functions digits रखने वाली एक string को एक int या double में convert करते हैं, अगर text एक valid number न हो तो std::invalid_argument throw करते हुए। std::to_string उल्टा करता है, display या concatenation के लिए एक number को इसके string representation में बदलते हुए।
उदाहरण: Numeric Conversions
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string input = "42";
// Declare number and set it to std::stoi(input)
int number = std::stoi(input);
std::string back = std::to_string(number * 2);
// Print to the console with cout
std::cout << back << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
std::stringको C-stylechar*strings से confuse करना।substrको length से बड़ा एक start index pass करना, जोstd::out_of_rangethrow करता है।findके result को-1की बजायstd::string::nposसे compare करना।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: