我想逐行读取文件并捕获一个特定的输入行.为了获得最大性能,我可以通过读取整个文件并使用指针迭代其内容以低级方式执行此操作,但此代码对性能不重要,因此我希望使用更具可读性和类型安全的标准库样式实现.
所以我拥有的是:
std::string line;
line.reserve(1024);
std::ifstream file(filePath);
while(file)
{
std::getline(file, line);
if(line.substr(0, 8) == "Whatever")
{
// Do something ...
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这不是性能关键代码,但我在解析操作之前调用了line.reserve(1024)来排除字符串的多次重新分配,因为读入较大的行.
在std :: getline里面,在添加每行的字符之前删除字符串.我逐步完成了这段代码,以满足自己每次迭代都没有重新分配内存,我发现这些内容让我的大脑充满了热情.
深入string :: erase而不是仅将其size变量重置为零实际上正在调用memmove_s,其指针值会覆盖缓冲区的使用部分,紧跟其后的缓冲区的未使用部分,除了memmove_s正在使用count参数调用零,即请求移动零字节.
问题:
为什么我想在我可爱的循环中调用库函数调用的开销,特别是那个被调用什么都没做的东西?
我自己还没有选择它,但在什么情况下,这个调用实际上什么都不做,但实际上会开始移动大块的缓冲区?
为什么这样做呢?
奖金问题:C++标准库标签是什么?
我有一个从Window派生的简单视图.在该派生类的代码隐藏文件中,我定义了一个名为ActiveDocument的新DependencyProperty.
我希望将这个新的DependencyProperty绑定到ViewModel上的一个属性,该属性被设置为视图的DataContext.
我可以使用类构造函数中的代码设置此绑定,但是尝试绑定XAML文件中的属性会导致出现错误消息,指出无法在类Window上找到属性ActiveDocument.
在XAML中执行此操作的正确语法是什么?
[使用代码更新]
MainWindowViewModel.cs
class MainWindowViewModel
{
public bool IWantBool { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = new MainWindowViewModel();
InitializeComponent();
}
public static readonly DependencyProperty BoolProperty = DependencyProperty.Register(
"BoolProperty", typeof(bool), typeof(MainWindow));
}
Run Code Online (Sandbox Code Playgroud)
Mainwindow.xaml
<Window x:Class="DependencyPropertyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DependencyPropertyTest"
<!-- ERROR: BoolProperty not found on type Window. -->
BoolProperty="{Binding path=IWantBool}"
<!-- ERROR: Attachable property not found in type MainWindow. -->
local:MainWindow.BoolProperty="{Binding path=IWantBool}">
<Grid>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud) VS2015 Update 3编译时没有错误,也没有警告.然而,我的印象是临时工具只能绑定到const引用.这是不符合的还是我误解了什么?
struct Foo {};
Foo Func6() { return Foo(); }
TEST(Arguments, NonConstReference)
{
Foo& bob = Func6();
}
Run Code Online (Sandbox Code Playgroud)
编辑
由marcinj链接的问题是同一个问题,虽然这个问题并不是特别关于VS2015,而是在我输入这个问题时找不到.
我用/ W4进行了测试,看看我是否收到了警告并没有看到,但是再次测试我发现现在我做了.当打开项目属性来调整设置时,VS2015习惯于显示与当前选择的构建配置不同的构建配置的属性对话框,这是一个让我多次抓到的最无益的行为,(我肯定会继续这样做).
正如在其他问题中提到的那样,使用禁用语言扩展(/ Za)会使这个错误,但遗憾的是它不是一个可用的解决方案,因为微软自己的平台头不会编译.
当我使用 Typescript 创建新的 Node 项目时,VsCode 自动导入建议根本不起作用。
重现步骤:
创建工作区目录。
运行npm init指定main.ts为入口点文件。
导入 typescript clinpm i typescript
创建 tsconfig.json.\node_modules\.bin\tsc --init
创建 main.ts 包含console.log('Running');
转译使用.\node_modules\.bin\tsc -w
Debug通过单击VsCode 并使用默认的 Nodejs 启动配置来运行。
导入库,例如 Rxjs 导入 typescript clinpm i rxjs
结果:
在 main.ts 中尝试使用任何 Rxjs 类型Observable、BehaviourSubject全局运算符from of sequenceEqual等都不会产生任何导入帮助。
我已阅读 VsCode Typescript 文档,没有任何迹象表明出了什么问题。https://code.visualstudio.com/docs/languages/typescript
我尝试在 tsconfig.json 中显式设置include和exclude目录,但这也没有效果。
我需要在 tsconfig 中手动设置一些模块解析选项吗?
我不知道,我不知道为什么这不起作用。
更新:
我设法通过指定使 VsCode 自动导入工作
"typeRoots": [ …Run Code Online (Sandbox Code Playgroud) 这是来自VS2012附带的C++标准库xutility头.
template<class _Elem1,
class _Elem2>
struct _Ptr_cat_helper
{ // determines pointer category, nonscalar by default
typedef _Nonscalar_ptr_iterator_tag type;
};
template<class _Elem>
struct _Ptr_cat_helper<_Elem, _Elem>
{ // determines pointer category, common type
typedef typename _If<is_scalar<_Elem>::value,
_Scalar_ptr_iterator_tag,
_Nonscalar_ptr_iterator_tag>::type type;
};
Run Code Online (Sandbox Code Playgroud)
具体来说,第二个_Ptr_cat_helper声明的性质是什么?声明符_Ptr_cat_helper之后的尖括号使它看起来像一个特化.但是,不是指定专门用于模板的完整或部分类型,而是仅重复模板参数多次.
我不认为我以前见过这个.它是什么?
UPDATE
我们都清楚,专业化适用于模板的实例化,其中两个模板参数的类型相同,但我不清楚它是构成完全或部分特化,还是为什么.
我认为当所有模板参数显式提供或由默认参数提供时,专门化是完全特化,并且完全用于提供实例化模板,相反,如果不是所有模板参数,则特殊化也是部分的由于专业化提供了一个或多个(但不是全部),和/或模板参数是否以专业化模式修改的形式使用,因此是必需的.例如
由于特化提供了至少一个但不是全部的模板参数,因此是一个部分的特化.
template<typename T, typename U>
class G { public: T Foo(T a, U b){ return a + b; }};
template<typename T>
class G<T, bool> { public: T Foo(T a, bool b){ return b ? ++a : a; …Run Code Online (Sandbox Code Playgroud) In an ASP.Net Core web service if an HttpClient.GetStringAsync call fails for 404 error or whatever, it throws an HttpRequestException. This is great.
If on the other hand an HttpClient.PostAsync call fails for 404, it does not throw an exception. I have to check the status code, fabricate an exception and throw it manually.
Why the discrepency, and is there a more elegant way of dealing with this?
HttpResponseMessage response = await _http.PostAsync("api/pollapi/request", new StringContent(requestInput));
if (!response.IsSuccessStatusCode)
{
throw new …Run Code Online (Sandbox Code Playgroud) 我在一些我看不懂的代码中遇到了这个声明.
typedef struct foo_* foo;
int main(int argc, char** argv)
{
foo a = nullptr; // Ok. foo is a synonym for 'foo_*'
foo_* b = a; // Ok! somehow foo_* is a valid type, but where is it defined?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
foo_之前未定义或声明该类型(这是一个完整的编译示例).我的理解是a typedef为已经存在的类型提供了同义词,并且在声明或定义之前不能使用类型.既然foo_以前没有宣布或定义过这是什么typedef,那怎么合法呢?
更新:
有人说这是可以的,因为typedef中的struct foo_*被假定为内联前向声明,并且实际上尝试使用foo_将失败.但我刚刚在VS2012上对此进行了测试,并且引用此类型对我来说很好.
当VS2015在一行代码中检测到错误时,它会显示红色波形.当您将鼠标光标悬停在其上方时,它会显示一个灯泡和一个错误弹出窗口,指出错误,此时您可以按CTRL +'.显示'潜在修复'并选择一个.
问题是将键盘光标移到红色波浪线上并按下CTRL +'.' 什么都不做.CTRL +'.' 键盘快捷键仅在灯泡和错误弹出窗口已经可见时才有效,并且使灯泡和弹出窗口可见的唯一方法是将鼠标光标悬停在红色波浪线上,此时(因为您已经不得不抓住鼠标) )它使用了CTRL +'.' 键盘快捷键相当无意义.
所以我的问题是:如何仅使用键盘访问"显示潜在修复"功能?
VS2015 Pro Update 3,已安装Resharper但已禁用.Win7 x64.