สตริงใน C และ C++
C เก็บข้อความในอาร์เรย์ char ที่ปิดท้ายด้วยอักขระ '\0' ที่มองไม่เห็น ส่วน C++ ห่อข้อความไว้ใน std::string ที่จัดการหน่วยความจำให้
สตริงใน C และ C++ คืออะไร?
C เก็บข้อความในอาร์เรย์ char ที่ปิดท้ายด้วยอักขระ '\0' ที่มองไม่เห็น ส่วน C++ ห่อข้อความไว้ใน std::string ที่จัดการหน่วยความจำให้
เทียบอาร์เรย์อักขระของ C กับ std::string ของ C++: ความยาว การต่อ การเปรียบเทียบ และการค้นหา
ประเด็นสำคัญ
- เปิด compiler warning และแก้ทุกคำเตือน
- รู้ type และ lifetime ของทุกค่า
- ตรวจ input และขอบเขต array ให้ชัดเจน
ตัวอย่างโค้ด C และ C++
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
/* A C string is a char array that ends with '\0'. */
char name[32] = "Ada";
printf("%s has %zu letters\n", name, strlen(name));
/* strcat appends; the array must have room for the result. */
strcat(name, " Lovelace");
printf("%s\n", name);
/* strcmp returns 0 when two strings are equal. */
printf("equal: %d\n", strcmp("cat", "cat") == 0);
/* Walk the characters until the terminating '\0'. */
char upper[32];
size_t i = 0;
for (; name[i] != '\0'; i++) {
upper[i] = (char)toupper((unsigned char)name[i]);
}
upper[i] = '\0';
printf("%s\n", upper);
/* strstr finds a substring and returns a pointer to it, or NULL. */
printf("contains \"love\": %s\n", strstr("lovelace", "love") ? "yes" : "no");
return 0;
}
Ada has 3 letters
Ada Lovelace
equal: 1
ADA LOVELACE
contains "love": yes
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
int main() {
// std::string manages its own memory and grows as needed.
std::string name = "Ada";
std::cout << name << " has " << name.size() << " letters\n";
name += " Lovelace";
std::cout << name << '\n';
// == compares the contents of two strings.
std::cout << "equal: " << (std::string("cat") == "cat") << '\n';
std::string upper = name;
std::transform(upper.begin(), upper.end(), upper.begin(),
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
std::cout << upper << '\n';
// find returns std::string::npos when the text is missing.
std::string word = "lovelace";
std::cout << "contains \"love\": " << (word.find("love") != std::string::npos ? "yes" : "no") << '\n';
std::cout << "first name: " << name.substr(0, 3) << '\n';
}
Ada has 3 letters
Ada Lovelace
equal: 1
ADA LOVELACE
contains "love": yes
first name: Ada
เปรียบเทียบ C และ C++
ใน C ฟังก์ชัน strlen, strcat, strcmp และ strstr ทำงานกับอาร์เรย์ดิบ และคุณต้องตรวจเองว่าอาร์เรย์ใหญ่พอ std::string มี size(), +=, ==, find() และ substr() และขยายเองอัตโนมัติ จึงเลี่ยงบัฟเฟอร์ล้นได้
C แสดง API แบบ procedural และรายละเอียด representation อย่างชัดเจน
C++ รักษา low-level model และเพิ่ม type ที่ปลอดภัยกว่า
แบบฝึกหัด
รันทั้งสองเวอร์ชันแล้วแก้ไขเพื่อสังเกตความแตกต่างของภาษา
- เพิ่ม input ผิดและค่าขอบเขต
- Compile ด้วย -Wall -Wextra -Wpedantic
- แยก declaration และ implementation
ฝึกด้วยโจทย์
ใช้สิ่งที่เรียนในบทนี้แก้ปัญหาจริง ทุกโจทย์รันในเบราว์เซอร์ พร้อมการทดสอบทันทีและรีวิวจาก AI
- Count Uppercase Letters Count the capital letters in a text. ง่าย
- Initials From a Name Build uppercase initials from a full name. ง่าย
- Replace Spaces With Dashes Turn spaces into dashes to make a URL-style slug. ง่าย