我有2个std :: string.我只是想,给定输入字符串:
为什么这样有效:
std::string s="hello";
std::string out;
std::transform(s.begin(), s.end(), std::back_inserter(out), std::toupper);
Run Code Online (Sandbox Code Playgroud)
但这不会(导致程序崩溃)?
std::string s="hello";
std::string out;
std::transform(s.begin(), s.end(), out.begin(), std::toupper);
Run Code Online (Sandbox Code Playgroud)
因为这有效(至少在相同的字符串上:
std::string s="hello";
std::string out;
std::transform(s.begin(), s.end(), s.begin(), std::toupper);
Run Code Online (Sandbox Code Playgroud) 说我有以下(非常简单)的代码。
#include <iostream>
int main() {
std::cout << std::stoi("12");
}
Run Code Online (Sandbox Code Playgroud)
这在g ++和clang上都可以编译。但是,它无法在MSVC上编译,并出现以下错误:
错误C2039:“ stoi”:不是“ std”的成员
错误C3861:“ stoi”:找不到标识符
我知道这std::stoi是<string>标头的一部分,大概前两个编译器作为标头的一部分<iostream>而后者不包含。根据C ++标准[res.on.headers]
C ++标头可以包括其他C ++标头。
对我来说,这基本上说所有三个编译器都是正确的。
当我的一个学生提交作业时,这个问题就出现了,TA标记为未编译。我当然去修理了。但是,我想防止将来发生此类事件。因此,有没有一种方法可以确定应该包括哪些头文件,而无需在每次检查的三个不同编译器上进行编译?
我能想到的唯一方法是确保对于每个std函数调用都存在一个适当的include。但是,如果您现有的代码长数千行,则搜索起来可能很乏味。有没有更简单/更好的方法来确保交叉编译器的兼容性?
三种编译器的示例:https : //godbolt.org/z/kJhS6U
我正在使用变换算法和std :: toupper来实现这一点,但这可以在一行中完成,像这样吗?
transform(s.begin(), s.end(), ostream_iterator<string>(cout, "\n"),std::toupper);
Run Code Online (Sandbox Code Playgroud)
我得到错误,所以我必须为此创建一个函数并使用transform调用它,或者我可以使用一些适配器?
任何人都可以告诉我这个程序中的错误是什么
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str = "Now";
transform(str.begin(), str.end(), str.begin(), toupper);
cout<<str;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
"no matching function for call to 'transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)'
compilation terminated due to -Wfatal-errors."
Run Code Online (Sandbox Code Playgroud) 我想了解为什么完全限定std::结果会导致下面的编译错误,使用std::transform.
#include <algorithm>
int main() {
std::string str;
std::transform(str.begin(), str.end(), str.begin(), std::tolower); // does not compile
std::transform(str.begin(), str.end(), str.begin(), ::tolower); // OK
}
Run Code Online (Sandbox Code Playgroud)
无法推导一元运算符的错误。
source>: In function 'int main()':
<source>:4:19: error: no matching function for call to 'transform(std::__cxx11::basic_string<char>::iterator, std::__cxx11::basic_string<char>::iterator, std::__cxx11::basic_string<char>::iterator, <unresolved overloaded function type>)'
4 | std::transform(str.begin(), str.end(), str.begin(), std::tolower);
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/algorithm:62,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4285:5: note: candidate: 'template<class _IIter, class _OIter, class _UnaryOperation> constexpr _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)'
4285 | …Run Code Online (Sandbox Code Playgroud)