C++里string如何定義函數?
3 回答

嚕嚕噠
TA貢獻1784條經驗 獲得超7個贊
#include<iostream> #include<string> std::string exchange(std::string x, std::string y) { return y + x; } template < class T> void _swap(T& a, T& b) { T c; c = a; a = b; b = c; } int main() { std::string a = "world" ; std::string b = "hello" ; std::cout<< a << " " << b<<std::endl; _swap<std::string>(a,b); std::cout<< a << " " << b<<std::endl; } |
程序輸出
==================
world hello
hello world
==================
你的函數出錯了,主要檢查三個地方
是否引入了相關的頭文件 比如 #include<string>
是否用using 引入了命名空間或者在使用的地方使用了命名空間,比如using std::string 或using namespace std 來引入命名空間,或者在代碼中使用 std::string
你是函數是否有正確的返回值,或者函數內部有錯誤。如果僅僅是聲名,那么不要用花括號。
- 3 回答
- 0 關注
- 1155 瀏覽
添加回答
舉報
0/150
提交
取消