字 符 串 操 作 是 一 个 不 小 的 主 题 ,在 标 准 C++中 ,string字 符 串 类 成 为 一个 标 准 ,之 所 以 抛 弃 char*的 字 符 串 而 选 用 C++标 准 程 序 库 中 的 string类 , 是 因 为 他 和 前 者 比 较 起 来 , 不 必 担 心 内 存 是 否 足 够 、字 符 串 长 度等 等 , 而 且 作 为 一 个 类 出 现 , 他 集 成 的 操 作 函 数 足 以 完 成 我 们 大 多 数情 况 下 的 需 要 . 下 面 我 们 首 先 从 一 些 示 例 开 始 学 习 下 string类 的 使 用 . 1) #include #include using namespace std; void main() { string s("hehe"); string s1="abcd"; cout< #include using namespace std; void main() { char chs[] = "hehe"; string s(chs); cout< #include using namespace std; void main() { char chs[] = "hehe"; string s(chs,1,3); //指 定 从 chs的 索 引 1开 始 ,最 后 复 制 3个字节 cout< #include using namespace std; void main() { string s1("hehe"); string s2(s1); cout< #include using namespace std; void main() { string s1("hehe",2,3); string s2(s1); cout< #include using namespace std; void main() { char chs[] = "hehe"; string s(chs,3); //将 chs前 3个字符作为初值构造 cout< #include using namespace std; void main() { string s(10,'k'); //分配 10个字符,初值都是'k' cout< #include using namespace std; void main() { string s(10,'k'); //分配 10个字符,初值都是'k' cout<