C++ में Strings का Array
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
char names[3][5]जैसे एक C-style array में लंबे words के लिए काफी जगह न होना।- एक C-style row को
=से एक string assign करना, जो allowed नहीं है। - strings की संख्या के लिए
sizeof(arr)इस्तेमाल करना।
- Arrays एक type की कई values store करते हैं, multi-dimensional arrays समेत, और इन्हें loop किया जा सकता है और functions को pass किया जा सकता है।
- C-style strings और
std::stringtext handle करते हैं, concatenation, length, और character access के साथ। - String methods, string और number conversion, और strings के arrays आपको text के collections के साथ काम करने में मदद करते हैं।
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