C++:
स्ट्रिंग को जोड़ना
How to: (कैसे करें:)
C++ में strings को concatenate करना बहुत सीधा है. चलिए कुछ examples देखते हैं:
#include <iostream>
#include <string> // C++ Standard Library का string class include करना जरूरी है
int main() {
std::string hello = "नमस्ते";
std::string world = " दुनिया";
// '+' operator का इस्तेमाल करके concatenate करना
std::string greeting = hello + world;
std::cout << greeting << std::endl; // "नमस्ते दुनिया" output होगा
// append() function का इस्तेमाल करें
std::string completeGreeting = hello;
completeGreeting.append(world);
std::cout << completeGreeting << std::endl; // "नमस्ते दुनिया" output होगा
return 0;
}Deep Dive (गहराई में जानकारी):
String concatenation का concept वास्तव में काफी पुराना है और यह शुरुआती programming languages से ही मौजूद है. C++ में strings को handling करने के कई तरीके हैं:
Historical context: C-style strings (
chararrays) का इस्तेमाल पुराने C++ या C में होता था, जहाँ strings को concatenate करने के लिएstrcatजैसे functions का प्रयोग होता था.Alternatives: Modern C++ में,
std::stringclass के साथ+,+=operators औरappend()method का उपयोग करना ज्यादा सुविधाजनक और सेफ है.Implementation details:
std::stringपर operations perform करते वक्त, memory management और efficiency का ख्याल रखा जाता है.append()मेथड एक्सिस्टिंग string के अंत में डायरेक्टली जोड़ता है, जिससे performance बेहतर हो सकती है.
See Also (संबंधित जानकारी):
- C++
std::stringके बारे में और पढ़ें: cppreference.com - String handling in C++: cplusplus.com
- C-style strings के बारे में और जानें: cprogramming.com