النصوص: std::string — أساسيات C++
النصوص: std::string
std::string من المكتبة القياسية (#include <string>) هي الطريقة الحديثة للتعامل مع النصوص في C++: تدير ذاكرتها تلقائياً وتكبر حسب الحاجة.
#include <string>
string name = "Ahmed";
string empty; // نص فارغ ""الطول والأحرف
name.size(); // 5 (أو name.length())
name[0]; // 'A' (بدون فحص الحدود)
name.at(0); // 'A' مع فحص الحدود: يرمي استثناء إذا تجاوزت
name[0] = 'a'; // النصوص في C++ قابلة للتعديل (بعكس Python)الدمج والمقارنة
string first = "Ahmed", last = "Ali", name = "Ahmed";
string a = "apple", b = "banana";
string full = first + " " + last;
full += "!";
if (name == "Ahmed") { cout << "متطابقان"; } // مقارنة المحتوى
if (a < b) { cout << a << " قبل " << b; } // مقارنة أبجدية"Hello" + " World" بين نصين حرفيين لا يعمل، لأن النصوص الحرفية ليست std::string؛ يجب أن يكون أحد الطرفين string.
دوال مفيدة
| الدالة | الوظيفة |
|---|---|
| find("x") | موقع أول ظهور، أو string::npos إذا لم يوجد |
| substr(pos, len) | جزء من النص |
| replace(pos, len, "new") | استبدال جزء |
| insert(pos, "x") / erase(pos, len) | إدراج وحذف |
| empty() | هل النص فارغ؟ |
| push_back(c) | إضافة حرف |
string email = "user@site.com";
size_t at = email.find('@');
if (at != string::npos) {
string user = email.substr(0, at);
}التحويل بين النصوص والأرقام
int n = stoi("42"); // نص ← int
double d = stod("3.5"); // نص ← double
string s = to_string(99); // رقم ← نصstoi("abc") ترمي استثناء invalid_argument.
المرور على الأحرف
string word = "Kodax";
for (char c : word) { cout << c << ' '; }دوال الأحرف في <cctype>: toupper، tolower، isdigit، isalpha، isspace.
⚠️ النصوص العربية
std::string تخزن بايتات وليس أحرفاً. الحرف العربي في ترميز UTF-8 يأخذ بايتين، لذلك string("سلام").size() تساوي 8 وليس 4، و word[0] نصف حرف. الدوال size و [] و substr آمنة بالكامل مع النصوص الإنجليزية؛ مع العربية اكتفِ بالطباعة والدمج والمقارنة والبحث عن نص كامل.
تمرين
اكتب برنامجاً يأخذ جملة إنجليزية ويطبع: عدد الكلمات، والجملة بأحرف كبيرة، وهل هي palindrome (تُقرأ نفسها معكوسة) بعد تجاهل المسافات.
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main() {
string first = "Ada";
string last = "Lovelace";
string full = first + " " + last;
cout << full << " (" << full.size() << " chars)" << endl;
// الوصول والتعديل
full[0] = 'a';
cout << full << " | first char: " << full.at(0) << endl;
// البحث والتقطيع
string email = "student@kodax.app";
size_t at = email.find('@');
if (at != string::npos) {
cout << "user: " << email.substr(0, at) << endl;
cout << "domain: " << email.substr(at + 1) << endl;
}
if (email.find("gmail") == string::npos) {
cout << "not gmail" << endl;
}
// التحويل
int age = stoi("21");
double price = stod("4.75");
string label = "Total: " + to_string(age * price);
cout << label << endl;
// تحويل لأحرف كبيرة
string shout = "hello c++";
for (char& c : shout) c = toupper(static_cast<unsigned char>(c));
cout << shout << endl;
// palindrome
string word = "racecar";
string reversed(word.rbegin(), word.rend());
cout << word << (word == reversed ? " is" : " is not") << " a palindrome" << endl;
// تنبيه: النص العربي بايتات
string arabic = "سلام";
cout << "bytes in Arabic word: " << arabic.size() << endl; // 8
return 0;
}
اختبر فهمك