比如說:string="abcde"; 有沒有可以提取其中某個字符的函數,比如提取第三個‘c’. 另外, #include <iostream> #include <string> using namespace std; void main() { string s="meum"; int n=strlen(&s); //@ cout<<s[1]<<endl; if(s[3]==101) cout<<"hhh"<<endl; else cout<<"55"; } 小弟先謝過了,:-)
2 回答

梵蒂岡之花
TA貢獻1900條經驗 獲得超5個贊
nt n = strlen(s.c_str())才對。
s是string類型,不能被strlen函數直接處理,要轉換成C風格的字符串,就用c_str()方法。
但是轉換后是一個const指針,要注意使用。

叮當貓咪
TA貢獻1776條經驗 獲得超12個贊
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a="asfsadf";
cout<<a[4]<<endl;
system("pause");
return 0;
}///
///
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s="meum";
int n=strlen(s.c_str());
cout<<s[1]<<endl;
if(s[3]==101)
cout<<"hhh"<<endl;
else
cout<<"55";
system("pause");
return 0;
}
- 2 回答
- 0 關注
- 120 瀏覽
添加回答
舉報
0/150
提交
取消