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

C++ में Strings का Array

Strings का एक array words की एक list है, कई labeled jars रखने वाले एक shelf जैसा। आप हर word को इसके number से पहुंचते हैं।
Syntax
cpp
char array_name[count][max_length] = {"text1", "text2"};

std::string array_name[count] = {"text1", "text2"};

Strings का C-style Array

Strings का एक C-style array एक 2D character array की तरह declare किया जा सकता है, जैसे char names[3][10];, जहाँ पहला dimension बताता है कि आपके पास कितनी strings हैं और दूसरा cap करता है कि हर individual string (terminator समेत) कितने characters रख सकती है।

यह rigid per-string size limit exactly वह है जिससे modern C++ बचने की कोशिश करता है।

उदाहरण: C-style Array of Strings

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	char names[3][10] = {"Alice", "Bob", "Carol"};
	// Print to the console with cout
	std::cout << names[0] << " " << names[1] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

C++ std::string Array

Modern approach std::string objects का एक ordinary array declare करता है, जैसे string names[3];, जहाँ हर element अपनी memory खुद manage करता है और ज़रूरत के मुताबिक कितनी भी length तक बढ़ सकता है — per string किसी fixed character limit की चिंता नहीं, C-style 2D array approach के उलट।

उदाहरण: C++ std::string Array

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string names[3];
	names[0] = "Alice";
	// Print to the console with cout
	std::cout << names[0] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

String Arrays Initialize करना

आप declaration पर ही एक initializer list इस्तेमाल करके एक string array populate कर सकते हैं, जैसे string days[] = {"Mon", "Tue", "Wed"};, और numeric arrays की तरह ही, अगर आप brackets खाली छोड़ें तो compiler array को size करने के लिए automatically elements count करेगा।

उदाहरण: Initializing String Arrays

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string days[] = {"Mon", "Tue", "Wed"};
	// Print to the console with cout
	std::cout << days[1] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Strings Access और Modify करना

Array के अंदर individual strings को एक single index से access किया जाता है, जैसे names[1], और क्योंकि हर element एक पूरा std::string है, आप इस पर तुरंत कोई भी string method call कर सकते हैं, जैसे names[1].append("son");, उस specific entry को in place modify करने के लिए।

उदाहरण: Accessing and Modifying Strings

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string names[] = {"Alice", "Bob"};
	names[1] = "Bobby";
	// Print to the console with cout
	std::cout << names[1] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

String Arrays के जरिए Iterate करना

एक regular indexed for loop या एक range-based for loop दोनों एक string array में चल सकते हैं — indexed version useful है जब आपको हर string की position भी जाननी हो, जबकि range-based version cleaner है जब आपको सिर्फ string values की परवाह हो।

उदाहरण: Iterating Through String Arrays

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string days[] = {"Mon", "Tue", "Wed"};
	for (const std::string &day : days) {
		// Print to the console with cout
		std::cout << day << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	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. char names[3][5] जैसे एक C-style array में लंबे words के लिए काफी जगह न होना।
  2. एक C-style row को = से एक string assign करना, जो allowed नहीं है।
  3. strings की संख्या के लिए sizeof(arr) इस्तेमाल करना।
चैप्टर सारांश
  • Arrays एक type की कई values store करते हैं, multi-dimensional arrays समेत, और इन्हें loop किया जा सकता है और functions को pass किया जा सकता है।
  • C-style strings और std::string text handle करते हैं, concatenation, length, और character access के साथ।
  • String methods, string और number conversion, और strings के arrays आपको text के collections के साथ काम करने में मदद करते हैं।

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.