← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 8 of 15

C++ का std::string

std::string C++ का easy-to-use text type है, letters की एक stretchy necklace की तरह। यह आपके लिए length और pieces जोड़ने का काम handle करता है।
Syntax
cpp
#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?

cpp
#include <iostream>
#include <string>

int main() {
	std::string name = "Alex"; // grows/shrinks automatically, no fixed buffer size
	std::cout << name << std::endl;
	return 0;
}

Declare और Initialize करना

एक std::string empty बनाया जा सकता है (string s;), सीधे एक literal से initialize किया जा सकता है (string s = "hello";), या एक constructor से बनाया जा सकता है जो एक character को एक set संख्या में दोहराता है (string s(5, '*'); "*****" बनाता है), आप जो बना रहे हैं उसके हिसाब से flexibility देते हुए।

उदाहरण: Declaring and Initializing

cpp
// 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;
}

String Concatenation

+ और += operators आपको strings को dynamically साथ जोड़ने देते हैं, जैसे string full = firstName + " " + lastName;, ज़रूरत पड़ने पर resulting string की memory automatically बढ़ाते हुए — जिसके लिए C-style strings के साथ careful manual buffer management चाहिए होगी।

उदाहरण: String Concatenation

cpp
// 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;
}

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

cpp
#include <iostream>
#include <string>

int main() {
	std::string name = "Alex"; // cin >> would stop at the first space
	std::cout << name << std::endl;
	return 0;
}

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

cpp
#include <iostream>
#include <string>

int main() {
	std::string name = "Alex";
	std::cout << name[0] << std::endl; // [] performs no bounds checking
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. cin >> s से एक पूरा sentence पढ़ना, जो पहले whitespace पर रुकता है; पूरी lines के लिए getline(cin, s) इस्तेमाल करें।
  2. #include <string> भूल जाना और std::string available होने की उम्मीद करना, जो कुछ compilers पर compile होने में fail हो सकता है।
  3. दो string literals के साथ "Hello" + " World" लिखना, जो compile नहीं होता क्योंकि कोई भी side std::string नहीं है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.