我有一个整数向量,可以说{1, 2, 3, 4}。
如何10为每个元素添加常量值以将向量修改为{11, 12, 13, 14}。
如果我想将每个元素都除以an int并修改向量,则与除法相同。我一直找不到解决方案。
I wanted to know if a variable can be equal to a type (here it's the magic_type)
#include <typeinfo>
template<typename T>
class C
{
public:
magic_type t;
t list;
T data;
C(void)
{
if (typeid(data) == typeid(int))
t = float; // so typeid(list) = typedid(float)
else
t = int; // and typeid(list) = typedid(int)
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个小问题。我想利用字符串中的双字母大写。我设法编译了一个程序,但没有成功。
#include <iostream>
#include <cctype>
#include <string>
std::string::iterator function(
std::string::const_iterator a,
std::string::const_iterator b,
std::string::const_iterator e)
{
for (; a < b; a++)
{
if (*a == *(a + 1))
{
toupper(*a);
toupper(*(a + 1));
}
}
}
int main()
{
std::string in = "peppermint 1001 bubbles balloon gum", out(100, '*');
auto e = function(in.cbegin(), in.cend(), out.begin());
int n = e - out.begin();
std::string s = out.substr(0, n);
bool b = (s == "pePPermint 1001 buBBles baLLOOn gum");
std::cout << std::boolalpha …Run Code Online (Sandbox Code Playgroud) 回顾微软的STL的代码(具体来说std::vector),我发现了以下几行代码(无关代码替换为/* ... */):
// CLASS TEMPLATE vector
template <class _Ty, class _Alloc = allocator<_Ty>>
class vector // varying size array of values
{
/* ... */
public:
/* ... */
using value_type = _Ty;
using allocator_type = _Alloc;
using pointer = typename _Alty_traits::pointer;
using const_pointer = typename _Alty_traits::const_pointer;
using reference = _Ty&;
using const_reference = const _Ty&;
using size_type = typename _Alty_traits::size_type;
using difference_type = typename _Alty_traits::difference_type;
/* ... */
};
Run Code Online (Sandbox Code Playgroud)
我想知道为什么这里使用为模板类型分配类型别名的约定?
我想编写一个函数,该函数采用任何类型的通用容器并打印它。
让我们暂时搁置它不适用于某些关联容器并关注这个问题:
template<template<typename> typename Cont, typename T>
void print(const Cont<T>& cont)
{
for (const auto it : cont)
{
cout << it << " ";
}
cout << endl;
}
int main()
{
vector<string> v;
print(v);
}
Run Code Online (Sandbox Code Playgroud)
错误指出:
template<template<typename> typename Cont, typename T>
void print(const Cont<T>& cont)
{
for (const auto it : cont)
{
cout << it << " ";
}
cout << endl;
}
int main()
{
vector<string> v;
print(v);
}
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么编译器不能在这里推断出类型?
即使我明确声明print<vector<string>>(v)?
我需要获得一个基于偏移量的迭代器。
即有开始的迭代器:
auto it = set.begin()
Run Code Online (Sandbox Code Playgroud)
我需要到达具有偏移量的迭代器ofst:
it + ofst
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?我需要增量增加it++迭代器ofst次数。
我想为STL容器设计一个打印功能,包括:std::vector, std::map, std::unodered_map, std::set, std::unordered_set, std::list....
理想的函数如下所示:
template<typename T>
void Show(const T& t)
{
if (istype(t, std::vector))
{
// here is a presudo code
// print vector
for (auto i: t) cout << i << endl;
}
else if (istype(t, std::unordered_map))
{
// print unordered_map
}
else
{ }
}
Run Code Online (Sandbox Code Playgroud)
我认为问题发生在 istype()
我知道有一些功能std::is_same可以做到这一点。
但是好像只能操作int或者float不能操作std::vector,你能帮帮忙吗?
以下原型旨在进行同步打印:
#include <iostream>
#include <string>
#include <sstream>
#include <mutex>
#include <Windows.h> // for OutputDebugString
std::mutex sync_mutex;
template<typename T>
void sync_print_impl(std::ostringstream& str, const T& t)
{
str << t << " ";
}
template<typename ... Args>
void sync_print_impl(std::ostringstream& str, Args ... args)
{
str; // prevents unused variable warning when sync_print is called without arguments
(..., sync_print_impl(str, args));
}
template <typename... Args>
void sync_print(Args... args)
{
std::ostringstream s;
sync_print_impl(s, args...);
{
std::lock_guard<std::mutex> lg(sync_mutex);
std::cout << s.str() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
简单测试即可: …
在下面的代码中,我使用模板函数和类型特征来区分整数类型(其他情况)和数组类型。我希望输出分别为int和array,而是分别int int使用 int 类型和数组类型实例化模板函数的两个调用:
这是为什么?
#include <iostream>
#include <array>
template <typename T>
inline static void constexpr SetCoordinates()
{
if (std::is_array<T>::value)
std::cout<<"array\n";
else
std::cout<<"int\n";
}
int main()
{
int a = 6;
std::array<int, 4> arr = {1,2,3,4};
SetCoordinates<decltype(a)>();
SetCoordinates<decltype(arr)>();
return 0;
}
Run Code Online (Sandbox Code Playgroud) typename在函数的返回类型之前使用“”与在函数声明中不使用它(如下所示)有什么区别?
如果我们根本不使用它,会有什么不同呢?
template< class T > typename std::remove_reference<T>::type&& move( T&& t );
template< class T > std::remove_reference_t<T>&& move( T&& t ) ;
Run Code Online (Sandbox Code Playgroud)