C++ 11添加了一些新的字符串转换函数:
http://en.cppreference.com/w/cpp/string/basic_string/stoul
它包括stoi(string to int),stol(string to long),stoll(string to long long),stoul(string to unsigned long),stoull(string to unsigned long long).值得注意的是它是一个stou(字符串到无符号)函数.有什么理由不需要它,但所有其他的都是吗?
ifstream f;
f.open(fileName);
if ( f.fail() )
{
// I need error message here, like "File not found" etc. -
// the reason of the failure
}
Run Code Online (Sandbox Code Playgroud)
如何将错误消息作为字符串?
有没有办法用另一个字符串替换所有出现的子字符串std::string?
例如:
void SomeFunction(std::string& str)
{
str = str.replace("hello", "world"); //< I'm looking for something nice like this
}
Run Code Online (Sandbox Code Playgroud) 我经常看到使用uint32,uint64等类型的源代码,我想知道它们是应该由程序员在应用程序代码中定义,还是在标准的lib头文件中定义.
在我的应用程序源代码上使用这些类型的最佳方法是什么?
我最近修改了一些代码,并在函数中的一行上发现了一个预先存在的错误:
std:;string x = y;
Run Code Online (Sandbox Code Playgroud)
此代码仍然编译并一直按预期工作.
字符串定义是有效的,因为这个文件是using namespace std;,因此std::首先是不必要的.
问题是,为什么std:;编译以及它做了什么,如果有的话?
以下是如何实现的std::is_function?
template<class T>
struct is_function : std::integral_constant<
bool,
!std::is_const<const T>::value && !std::is_reference<T>::value
> {};
Run Code Online (Sandbox Code Playgroud)
(来自CPP 参考)
在我看来, anint将是此定义下的函数。我错过了什么?
我正在尝试编译使用g++和-std=c++11或c++0x标志.
但是,我收到此错误
cc1plus: error: unrecognized command line option "-std=c++11"
Run Code Online (Sandbox Code Playgroud)
g ++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud) 值一千字:
#include<string>
#include<iostream>
class SayWhat {
public:
SayWhat& operator[](const std::string& s) {
std::cout<<"here\n"; // To make sure we fail on function entry
std::cout<<s<<"\n";
return *this;
}
};
int main() {
SayWhat ohNo;
// ohNo[1]; // Does not compile. Logic prevails.
ohNo[0]; // you didn't! this compiles.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将数字0传递给接受字符串的方括号运算符时,编译器不会抱怨。相反,它会在输入以下方法之前编译并失败:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Run Code Online (Sandbox Code Playgroud)
以供参考:
> g++ -std=c++17 -O3 -Wall -Werror -pedantic test.cpp -o test && ./test
> g++ --version
gcc …Run Code Online (Sandbox Code Playgroud) C++ 0x添加hash<...>(...).
我找不到一个hash_combine函数,如boost中所示.实现这样的事最简洁的方法是什么?也许,使用C++ 0x xor_combine?
我一直在尝试在C++中找到两个std :: set之间的交集,但我一直收到错误.
我为此创建了一个小样本测试
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main() {
set<int> s1;
set<int> s2;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
s2.insert(1);
s2.insert(6);
s2.insert(3);
s2.insert(0);
set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
后一个程序不生成任何输出,但我希望有一个新的集合(让我们称之为s3)具有以下值:
s3 = [ 1 , 3 ]
Run Code Online (Sandbox Code Playgroud)
相反,我得到错误:
test.cpp: In function ‘int main()’:
test.cpp:19: error: no matching function for call to ‘set_intersection(std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>)’
Run Code Online (Sandbox Code Playgroud)
我从这个错误中理解的是,没有定义set_intersection接受Rb_tree_const_iterator<int>参数.
此外,我想该std::set.begin()方法返回这种类型的对象,
有没有更好的方法std::set在C++中找到两个的交集?最好是内置功能?
非常感谢!