小编JeJ*_*eJo的帖子

向向量的所有元素添加一个整数

我有一个整数向量,可以说{1, 2, 3, 4}

如何10为每个元素添加常量值以将向量修改为{11, 12, 13, 14}

如果我想将每个元素都除以an int并修改向量,则与除法相同。我一直找不到解决方案。

c++ algorithm add stdvector c++11

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

A variable (or an attribute) can be equal to a type?

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)

c++ templates types class-template c++17

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

大写字母

我有一个小问题。我想利用字符串中的双字母大写。我设法编译了一个程序,但没有成功。

#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)

c++ algorithm iterator loops stdstring

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

为什么要在 C++ 标准容器中为模板参数添加别名?

回顾微软的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)

我想知道为什么这里使用为模板类型分配类型别名的约定?

c++ templates class std type-alias

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

为什么编译器不能推导出模板模板参数?

我想编写一个函数,该函数采用任何类型的通用容器并打印它。
让我们暂时搁置它不适用于某些关联容器并关注这个问题:

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)

c++ templates template-templates function-templates c++11

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

如何根据 set.begin() 的偏移量迭代 std::set?

我需要获得一个基于偏移量的迭代器。

即有开始的迭代器:

auto it = set.begin()
Run Code Online (Sandbox Code Playgroud)

我需要到达具有偏移量的迭代器ofst

it + ofst
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?我需要增量增加it++迭代器ofst次数。

c++ algorithm iterator stdset c++11

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

如何在模板函数中实现模板类类型?

我想为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,你能帮帮忙吗?

c++ templates c++-standard-library function-templates c++17

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

带有折叠表达式的模板会创建不需要的参数副本

以下原型旨在进行同步打印:

#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)

简单测试即可: …

c++ templates function-templates fold-expression c++17

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

为什么模板函数中的 std::is_array 不区分 int 和 array 类型?

在下面的代码中,我使用模板函数和类型特征来区分整数类型(其他情况)和数组类型。我希望输出分别为intarray,而是分别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)

c++ templates type-traits function-templates c++11

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

下面使用“typename”有什么区别?

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)

c++ templates return-type typename function-templates

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