让我们有一个名为Y的函数重载:
void Y(int& lvalue)
{ cout << "lvalue!" << endl; }
void Y(int&& rvalue)
{ cout << "rvalue!" << endl; }
Run Code Online (Sandbox Code Playgroud)
现在,让我们定义一个像std :: forward一样的模板函数
template<class T>
void f(T&& x)
{
Y( static_cast<T&&>(x) ); // Using static_cast<T&&>(x) like in std::forward
}
Run Code Online (Sandbox Code Playgroud)
现在看看main()
int main()
{
int i = 10;
f(i); // lvalue >> T = int&
f(10); // rvalue >> T = int&&
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,输出是
lvalue!
rvalue!
Run Code Online (Sandbox Code Playgroud)
现在回到模板功能f()并替换static_cast<T&&>(x)为static_cast<T>(x).让我们看看输出:
lvalue!
rvalue!
Run Code Online (Sandbox Code Playgroud)
一样的!为什么?如果它们是相同的,那么为什么std::forward<>从返回一个投x来 …
考虑这个简单的程序:
vector<int> foo = {0, 42, 0, 42, 0, 42};
replace(begin(foo), end(foo), foo.front(), 13);
for(const auto& i : foo) cout << i << '\t';
Run Code Online (Sandbox Code Playgroud)
当我写它时我希望得到:
13 42 13 42 13 42
但相反,我得到了:
13 42 0 42 0 42
问题当然是replace通过引用获取最后2个参数.因此,如果它们中的任何一个碰巧处于正在操作的范围内,结果可能是意外的.我可以通过添加临时变量来解决这个问题:
vector<int> foo = {0, 42, 0, 42, 0, 42};
const auto temp = foo.front();
replace(begin(foo), end(foo), temp, 13);
for(const auto& i : foo) cout << i << '\t';
Run Code Online (Sandbox Code Playgroud)
我知道C++ 11为我们提供了各种类型的工具,我可以简单地将这个值强制转换为非引用类型并传递内联,而不创建临时的吗?
这个问题是关于C++ builder 6的代码.赏金对标准C++算法感兴趣,以便在给定标准化输入的情况下解决问题(有关更多信息,请参阅此内容.)
txt文件也代表我在数组中的数据:
1101 0110 1101 0110 1100 0101 0110
1110 1001 0110 1011 1010 1111 1010
1000 0101 0011 1110 1011 1110 1010
1011 1101 0101 0001 0101 0011 1011
txt的说明:
txt文件中的数字是房间墙壁的4位表示,其中一个位表示墙.壁位按顺时针顺序开始,最重要的位是西墙.例如,1101代表一个房间,其中:
鉴于:
numeric_limits<int>::max()房间我被要求发布我的代码,所以这里是:
我试图解决这个问题,但我得到EAAccessviolation有人可以告诉我我做错了什么?
int rn=0,z=0, global=0,coord[15],c[411],b1[411];
void peruse ( int i, int j,int* bb)
{
bool top=false,bottom=false,right=false,left=false;
//truth checks
if (bb[i*m+j]<1000) left=true;
if (bb[i*m+j]<100) top=true; …Run Code Online (Sandbox Code Playgroud)