我为我的一位学生同事编写了这段小代码,以说明 C++ 中的重载:
#include <iostream>
int main() {
std::cout << "Hello World!";
std::cout.operator<<("Hello World!");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我天真地以为我会有两次“Hello World!”。但是第二行给我发了一个地址。我不明白为什么?
我写了这段代码是为了检查 a 是否std::map包含特定的键:
template<typename T, typename... Args >
inline bool contains(const std::map<Args...>& map, const T& value) noexcept
{
static_assert(std::is_constructible_v< decltype(map)::key_type , T >);
return map.find(value) != std::end(map);
}
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
错误:
key_type不是成员const std::map<std::__cxx11::basic_string<char>, Query>&
有什么问题decltype(map)::key_type?
我创建了一个个人 C++ 库,并尝试在其他程序中使用它。为了创建和编译我的库,我使用以下命令
cmake -G "Visual Studio 16 2019" -A x64 ..
cmake --build . --config Release --target INSTALL
Run Code Online (Sandbox Code Playgroud)
我的编译和安装没有问题,但在 Target-release.cmake 安装中看起来像这样:
#----------------------------------------------------------------
# Generated CMake target import file for configuration "Release".
#----------------------------------------------------------------
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Import target "containers" for configuration "Release"
set_property(TARGET containers APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(containers PROPERTIES
IMPORTED_IMPLIB_RELEASE "C:/Program Files (x86)/containers/lib/containers.lib"
IMPORTED_LOCATION_RELEASE "C:/Program Files (x86)/containers/bin/containers.dll"
)
list(APPEND _IMPORT_CHECK_TARGETS containers )
list(APPEND _IMPORT_CHECK_FILES_FOR_containers "C:/Program Files (x86)/containers/lib/containers.lib" "C:/Program Files (x86)/containers/bin/containers.dll" …Run Code Online (Sandbox Code Playgroud) 这段代码不能在我的电脑上编译(用最少的例子编辑):
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
template<class PValues>
inline bool equal(PValues it,
PValues last,
size_t cols,
size_t rows,
size_t offset)
{
if(std::distance(it,last)<offset)
return false;
for(size_t c=0; c<cols; ++c)
{
auto p = it + c*rows;
if(*p != *(p + offset))
return false;
}
return true;
};
int main()
{
std::vector<std::string> vec = {"1","1","1","2","2","2","3","8","8",
"5","5","2","5","5","5","6","8","8",
"3","3","3","4","4","3","9","8","8"};
std::vector<double> nbs = {1,2,3,4,5,6,7,8,9};
auto res = equal(vec.data(), vec.data()+9, 3, 9, 1);
}
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
In file included from /usr/include/c++/9/bits/char_traits.h:39, …Run Code Online (Sandbox Code Playgroud) 我尝试制作一个通用的交叉产品功能:
template<class ContainerType1, class ContainerType2, typename ReturnType>
std::vector<ReturnType> cross_product(const ContainerType1& a, const ContainerType2& b)
{
assert((a.size()==3)&&(b.size==3));
return {a[1]*b[2]-a[2]-b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]};
}
Run Code Online (Sandbox Code Playgroud)
这条线
std::vector<double> A = cross_product(p_r2,p_r1);
Run Code Online (Sandbox Code Playgroud)
给我错误:
error : couldn't deduce template parameter ‘ReturnType’
Run Code Online (Sandbox Code Playgroud)
有没有办法保持通用性,并避免将ReturnType声明为例如double?
我编写了以下测试:
#include <cassert>
#include <iostream>
#include <string>
#include <cmath>
#include <functional>
// Works with std::function !
//std::function<double(double)> set_unary_operation(const std::string& unary)
auto set_unary_operation(const std::string& unary)
{
if(unary == "EXP")
return [](const double& x){ return std::exp(x);};
if(unary == "LOG")
return [](const double& x){ return std::log10(x);};
if(unary == "_LN")
return [](const double& x){ return std::log(x);};
if(unary == "ABS")
return [](const double& x){ return std::abs(x);};
return [](const double& x){return x;};
}
bool is_equal(double&& value, double&& test)
{
double epsilon = 0.0000001;
return (value-epsilon < …Run Code Online (Sandbox Code Playgroud)