2 回答

TA貢獻1820條經驗 獲得超10個贊
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream in("a.in");
ofstream out("a.out");
for (string str; getline(in, str);)
out << str << endl;
}

TA貢獻1789條經驗 獲得超10個贊
需要和ref對比著來說,out是返回值,使用ref前必須對變量賦值,out不用。
out的函數會清空變量,即使變量已經賦值也不行,退出函數時所有out引用的變量都要賦值,ref引用的可以修改,也可以不修改。
區(qū)別可以參看下面的代碼:
using System;
class TestApp
{
static void outTest(out int x, out int y)
{//離開這個函數前,必須對x和y賦值,否則會報錯。
//y = x;
//上面這行會報錯,因為使用了out后,x和y都清空了,需要重新賦值,即使調用函數前賦過值也不行
x = 1;
y = 2;
}
static void refTest(ref int x, ref int y)
{
x = 1;
y = x;
}
public static void Main()
{
//out test
int a,b;
//out使用前,變量可以不賦值
outTest(out a, out b);
Console.WriteLine("a={0};b={1}",a,b);
int c=11,d=22;
outTest(out c, out d);
Console.WriteLine("c={0};d={1}",c,d);
//ref test
int m,n;
//refTest(ref m, ref n);
//上面這行會出錯,ref使用前,變量必須賦值
int o=11,p=22;
refTest(ref o, ref p);
Console.WriteLine("o={0};p={1}",o,p);
}
}
- 2 回答
- 0 關注
- 110 瀏覽
添加回答
舉報