有一个数组:
char* arr[some_size] = {nullptr};
Run Code Online (Sandbox Code Playgroud)
然后初始化它的一些元素是否有任何方法可以以其他方式重置此数组,而不是迭代其元素并将它们设置为nullptr?
我想避免这个:
for(unsigned i = 0; i < some_size;++i)
{
arr[i] = nullptr;
}
Run Code Online (Sandbox Code Playgroud) 我在我的一篇帖子中提出了一个关于替代方案的问题boost::lexical_cast,在许多答复中,我有一个建议stoi作为可行的替代方案。
我决定测试它,令我惊讶的是,这个函数的第二个参数(描述大小的参数)是一个指向size_t类型的指针,而不是实际size_t类型。对此是否有任何合乎逻辑的解释,以及以何种方式拥有指向实际对象的指针比对象本身更好(仅在涉及大小的这种特殊情况下,我不会本能地用指针分配大小)?
链接到 stoi 文档:http : //msdn.microsoft.com/en-us/library/ee404860.aspx
有没有办法通过使用一些元编程技巧使情况像这样工作:
int* get();//this fnc returns pointer to int OR nullptr
int k = 1;
//this is the operator which is supposed to compare value and pointer
template<class T>
bool operator!=(const T& left, const T* right)
{
if (right)
{
return left != *right;
}
else
{
return false;
}
}
//And this is the code fragment which interests me most
if (k != get())
{
///
}
Run Code Online (Sandbox Code Playgroud)
关键是我不想改变这一行k!= get()但由于某种原因我的操作员!=似乎不起作用.有什么问题?
这段代码来自main:
Int<> a;
cout << typeid(Int<>::range_type).name();
Run Code Online (Sandbox Code Playgroud)
在使用gcc 4.6.1在代码块中编译时给出输出'x'.有什么理由吗?
template<class Int_T>
struct Best_Fit
{//evaluate it lazily ;)
typedef typename if_<std::is_signed<Int_T>::value,Signed_Type,Unsigned_Type>::type type;
};
template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range = std::numeric_limits<Int_T>::min(), typename Best_Fit<Int_T>::type Max_Range = std::numeric_limits<Int_T>::max()>
class Int {
public:
typedef decltype(Min_Range) range_type;
};
Run Code Online (Sandbox Code Playgroud) 伙计们我在VS尝试做类似的事情:
#ifdef _MSC_VER
#include "stdafx.h"
#endif
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误告诉我:
C1020: unexpected #endif
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
编辑
/ 这是stdafx.h的内容/
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
//#include <stdio.h>
//#include <tchar.h>
#include <iostream>
using std::cout;
using std::cerr;
// TODO: reference additional headers your program requires here
Run Code Online (Sandbox Code Playgroud) 这个最简单的程序在发布模式下编译时出错,但在调试模式下编译正常:
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc,argv);
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
错误(从 qt 的控制台输出)
*开始 C:\excercizes\QT_projects\Line_Counter-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release\release\Line_Counter.exe...程序意外完成。C:\excercizes\QT_projects\Line_Counter-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release\release\Line_Counter.exe 退出,代码为 -1073741511*
有人有过这种经历吗?
当我尝试在文件中找到最后一次出现的"EndProject"时,它看起来像这样:(这是原始VS解决方案文件,没有任何更改):
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ASO", "ASO\ASO.vcxproj", "{574117CD-377D-4C5A-8B6C-B0EAFF8CE158}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "asdfasdf", "asdfasdf\asdfasdf.vcxproj", "{05527F0D-4B98-4A55-B038-3C60005566CB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Debug|Win32.ActiveCfg = Debug|Win32
{574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Debug|Win32.Build.0 = Debug|Win32
{574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Release|Win32.ActiveCfg = Release|Win32
{574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Release|Win32.Build.0 = Release|Win32
{05527F0D-4B98-4A55-B038-3C60005566CB}.Debug|Win32.ActiveCfg = Debug|Win32
{05527F0D-4B98-4A55-B038-3C60005566CB}.Debug|Win32.Build.0 = Debug|Win32
{05527F0D-4B98-4A55-B038-3C60005566CB}.Release|Win32.ActiveCfg = Release|Win32
{05527F0D-4B98-4A55-B038-3C60005566CB}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Run Code Online (Sandbox Code Playgroud)
我正在使用:
auto end_of_project_manifest = project_file_contents.find_last_of("EndProject");//here project_file_contents is a …Run Code Online (Sandbox Code Playgroud) 在这段代码中:
for ( ;(auto i = std::find(some_string.begin(),some_string.end(),'%')) != some_string.end();)
{
}
Run Code Online (Sandbox Code Playgroud)
我从gcc 4.7.1收到错误:
error: invalid use of 'auto'|
Run Code Online (Sandbox Code Playgroud)
任何想法为什么?不应该正确编译?