数组的值如何在以下 C++ 代码中受到影响?
How the values of array got affected in the following c++ code?
编译以下程序时,输出为if break float while break。
#include<iostream>
using namespace std;
string s[5]={"if","int","float","while","break"};
string & blast(int i){ return s[i];}
int main()
{
for (int i = 0; i < 5; i++ )
if( i % 3 == 1 )
blast( i ) = s[ 5-i ];
for (int i = 0; i < 5; i++ )
cout << s[ i ] << " ";
cout<<endl;
return 0;
}
尝试:
blast[1] = s[4] = "break"
so, s[1] = "break"
Then blast[4] = s[4] = s[1] = "int"
但输出与此不符。
我没看懂这个..
请帮帮我。
正如你自己写的,当 i
等于 1
blast[1]= s[4] = "break"
so, s[1] = "break"
因此 s[1]
包含字符串 "break"
。之后数组不包含字符串 "int"
。然后这个字符串 "break"
现在从 s[1]
复制到 s[4]
当 i
等于 4
blast[4]= s[4] = s[1] = "break"
编译以下程序时,输出为if break float while break。
#include<iostream>
using namespace std;
string s[5]={"if","int","float","while","break"};
string & blast(int i){ return s[i];}
int main()
{
for (int i = 0; i < 5; i++ )
if( i % 3 == 1 )
blast( i ) = s[ 5-i ];
for (int i = 0; i < 5; i++ )
cout << s[ i ] << " ";
cout<<endl;
return 0;
}
尝试:
blast[1] = s[4] = "break"
so, s[1] = "break"
Then blast[4] = s[4] = s[1] = "int"
但输出与此不符。
我没看懂这个.. 请帮帮我。
正如你自己写的,当 i
等于 1
blast[1]= s[4] = "break"
so, s[1] = "break"
因此 s[1]
包含字符串 "break"
。之后数组不包含字符串 "int"
。然后这个字符串 "break"
现在从 s[1]
复制到 s[4]
当 i
等于 4
blast[4]= s[4] = s[1] = "break"