标签: std

为什么没有std :: stou?

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(字符串到无符号)函数.有什么理由不需要它,但所有其他的都是吗?

相关:C++ 11中没有"sto {short,unsigned short}"函数?

c++ string std c++11

92
推荐指数
2
解决办法
1万
查看次数

ifstream打开失败时如何获取错误消息

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)

如何将错误消息作为字符串?

c++ error-handling std stream

92
推荐指数
4
解决办法
9万
查看次数

如何在标准字符串中搜索/查找和替换?

有没有办法用另一个字符串替换所有出现的子字符串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)

c++ replace std

91
推荐指数
5
解决办法
10万
查看次数

是否在任何stdlib头中定义了uint32,int32,uint64,int64等类型?

我经常看到使用uint32,uint64等类型的源代码,我想知道它们是应该由程序员在应用程序代码中定义,还是在标准的lib头文件中定义.

在我的应用程序源代码上使用这些类型的最佳方法是什么?

c std

89
推荐指数
2
解决办法
19万
查看次数

什么会'std:;' 用c ++做什么?

我最近修改了一些代码,并在函数中的一行上发现了一个预先存在的错误:

std:;string x = y;
Run Code Online (Sandbox Code Playgroud)

此代码仍然编译并一直按预期工作.

字符串定义是有效的,因为这个文件是using namespace std;,因此std::首先是不必要的.

问题是,为什么std:;编译以及它做了什么,如果有的话?

c++ std colon

89
推荐指数
4
解决办法
2217
查看次数

std::is_function 是如何实现的?

以下是如何实现的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将是此定义下的函数。我错过了什么?

c++ templates std sfinae

88
推荐指数
2
解决办法
6212
查看次数

cc1plus:错误:使用g ++无法识别的命令行选项"-std = c ++ 11"

我正在尝试编译使用g++-std=c++11c++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)

c++ compiler-errors g++ std c++11

85
推荐指数
2
解决办法
19万
查看次数

防止函数使用const std :: string&接受0

值一千字:

#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++ string std implicit-conversion

80
推荐指数
2
解决办法
3816
查看次数

如何在C++ 0x中组合哈希值?

C++ 0x添加hash<...>(...).

我找不到一个hash_combine函数,如boost中所示.实现这样的事最简洁的方法是什么?也许,使用C++ 0x xor_combine

c++ hash boost std c++11

78
推荐指数
6
解决办法
3万
查看次数

如何在C++中找到两个std :: set的交集?

我一直在尝试在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++中找到两个的交集?最好是内置功能?

非常感谢!

c++ std stdset stl-algorithm

77
推荐指数
4
解决办法
6万
查看次数