有一个基于范围的for循环,其语法如下:
for(auto& i : array)
Run Code Online (Sandbox Code Playgroud)
它适用于常量数组,但不适用于基于指针的动态数组
int *array = new int[size];
for(auto& i : array)
cout<< i << endl;
Run Code Online (Sandbox Code Playgroud)
它给出了有关替换失败的错误和警告,例如:
错误] C:\ Users\Siegfred\Documents\C-Free\Temp\Untitled2.cpp:16:16:错误:没有匹配函数来调用'begin(int*&)'
如何在动态数组中使用这种新语法?
当我遇到这个奇怪的错误时,我正在尝试算法和lambdas:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> vec(10);
int OddCount;
for (int i = 1 ; i <= 10 ; ++i)
{
vec.push_back(i);
}
OddCount = count_if(vec.begin(),vec.end(),[](int v){return v%2 == 0;});
cout<<OddCount<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道向量vec,包含值1 - 10,当我使用count_if算法检查奇数时,它返回预期的数字,即5(1,3,5,7,9)但当我检查时偶数我得到的结果= 15,这很奇怪.这是怎么回事?
我试图以这种方式使用旧的bind2nd函数:
template<typename T>
class printer
{
public:
void operator()(T a, string& kd)
{
cout<<a<<endl;
}
};
int main(int argc, char *argv[])
{
string nme = "J-dar";
auto f1 = bind2nd(printer<int>(),nme);
//f1(5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我收到很多错误:
required from here
error: no type named 'first_argument_type' in 'class printer<int>' class binder2nd ^
error: no type named 'second_argument_type' in 'class printer<int>' typename _Operation::second_argument_type value; ^
error: no type named 'second_argument_type' in 'class printer<int>' binder2nd(const _Operation& __x, ^
error: no type named 'result_type' in 'class …Run Code Online (Sandbox Code Playgroud) 我尝试在500x500窗口中创建一个按钮,问题是,按钮没有出现在窗口中,单击Windows单独触发按钮的过程/处理程序:
#include <windows.h>
LRESULT CALLBACK MainWindowHandler(HWND obj, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK ButtonHandler(HWND obj, UINT msg, WPARAM wParam, LPARAM lParam);
LPCSTR FrameClassName = "MainWindow";
LPCSTR ButtonClassName = "Button";
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX Frame;
HWND FrameHandle;
WNDCLASSEX Button;
HWND ButtonHandle;
MSG Msg;
Frame.cbSize = sizeof(WNDCLASSEX);
Frame.style = 0;
Frame.lpfnWndProc = MainWindowHandler;
Frame.cbClsExtra = 0;
Frame.cbWndExtra = 0;
Frame.hInstance = hInstance;
Frame.hIcon = LoadIcon(NULL, IDI_APPLICATION);
Frame.hCursor = LoadCursor(NULL, IDC_ARROW);
Frame.hbrBackground = …Run Code Online (Sandbox Code Playgroud) 当我偶然发现一些MS头文件时......我找到了这样的代码
void App::OnSuspending(Object^ sender, SuspendingEventArgs^ args)
是一个普通的函数头,但是注意参数声明"Object ^","SuspendingEventArgs ^",我知道它们是某些类,但是在每个类之后使用"^"是什么?