亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

c++里面的string的repalce函數怎么用?

c++里面的string的repalce函數怎么用?

C++
MMMHUHU 2019-02-21 15:11:53
題目是,用字符串c替換掉字符串a中所有的b片段。#include <iostream>#include <string>#include <stdio.h>int main() {std::string a, b, c;std::cin >> a >> b >> c;while (a.find(b) != -1) {int i = a.find(b);a = a.replace(b, c);}std::cout << a << std::endl;return 0;} 為什么replace那行通不過編譯?謝謝指點哦。
查看完整描述

2 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
int main() {
std::string a, b, c;
std::cin >> a >> b >> c;
std::string::size_type len=b.length();
while (a.find(b) != -1) {
std::string::size_type i = a.find(b);
a.replace(i,len,c);
}
std::cout << a << std::endl;
return 0;
}

查看完整回答
反對 回復 2019-03-10
  • 2 回答
  • 0 關注
  • 859 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號