小编JeJ*_*eJo的帖子

除了 increment 语句外,如何制作 for 循环变量 const?

考虑一个标准 for 循环:

for (int i = 0; i < 10; ++i) 
{
   // do something with i
}
Run Code Online (Sandbox Code Playgroud)

我想防止变量ifor循环体中被修改。

但是,我不能声明iconst因为这会使增量语句无效。有没有办法在增量语句之外创建i一个const变量?

c++ algorithm variables for-loop constants

83
推荐指数
7
解决办法
4901
查看次数

有没有一种方法可以缩短这种情况?

while (temp->left->oper == '+' || 
       temp->left->oper == '-' || 
       temp->left->oper == '*' || 
       temp->left->oper == '/' || 
       temp->right->oper == '+' || 
       temp->right->oper == '-' || 
       temp->right->oper == '*' || 
       temp->right->oper == '/')
{
    // do something
}
Run Code Online (Sandbox Code Playgroud)

为了清楚起见:temp是一个指向以下node结构的指针:

struct node
{
    int num;
    char oper;
    node* left;
    node* right;
};
Run Code Online (Sandbox Code Playgroud)

c++ algorithm if-statement while-loop simplify

50
推荐指数
6
解决办法
6566
查看次数

如何使用标准库迭代相等的值?

假设我有一个向量:

std::vector<Foo> v;
Run Code Online (Sandbox Code Playgroud)

此向量已排序,因此相等的元素彼此相邻。

获得所有表示具有相等元素的范围的迭代器对的最佳方法是什么(使用标准库)?

while (v-is-not-processed) {
    iterator b = <begin-of-next-range-of-equal-elements>;
    iterator e = <end-of-next-range-of-equal-elements>;

    for (iterator i=b; i!=e; ++i) {
        // Do something with i
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何在上面的代码中获取b和的值e

因此,例如,如果v包含以下数字:

 index 0 1 2 3 4 5 6 7 8 9
 value 2 2 2 4 6 6 7 7 7 8
Run Code Online (Sandbox Code Playgroud)

然后,我想在循环中具有be指向元素:

 iteration  b  e
 1st        0  3
 2nd        3  4
 3rd        4  6
 4th        6  9
 5th …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm c++-standard-library iterator-range c++17

39
推荐指数
5
解决办法
1827
查看次数

std::vector 如何访问它们之间有巨大差距的元素?

有这个代码:

template <class IIt, class OIt>
OIt copy2(IIt begin, IIt end, OIt dest)
{
   while (begin != end)
   {
      //make gap between element addresses
      for (int i = 0; i < 99999999; i++)
      {
         dest++;
      }
      *dest++ = *begin++;
   }
   return dest;
}

int main(int argc, char** argv)
{
   vector<int> vec({ 1, 2, 3 });
   vector<int> vec2;
   copy2(vec.begin(), vec.end(), back_inserter(vec2));
   for (int i : vec2)
   {
      cout << i << endl;
   }
}
Run Code Online (Sandbox Code Playgroud)

编译需要很长时间,但最终会得到正确的输出

1
2
3
Run Code Online (Sandbox Code Playgroud)

问题是(在不知道 的内部实现的情况下 …

c++ algorithm templates iterator stdvector

39
推荐指数
3
解决办法
2093
查看次数

我们可以使用具有多个返回类型的函数吗?(在C ++ 11及更高版本中)

我试图写一个通用的函数,该函数的输入为uint8uint16uint32uint64,...,并与最大的元素的数据类型返回最大值?

例如:

template < typename T, typename X>
auto Max_Number(T valueA, X valueB)
{
    if (valueA > valueB)
        return valueA;
    else
        return valueB;
}
Run Code Online (Sandbox Code Playgroud)

PS:此示例假定最大元素具有最大数据类型。

c++ templates function return-type c++11

31
推荐指数
3
解决办法
1659
查看次数

如果 constexpr 失败,为什么 C++17 的这种用法会失败?

我正在尝试使用 C++17if constexpr进行条件编译,但它的行为并不符合我的预期。

比如下面的代码,C++还是编译宏定义的代码X2

#include <map>
#include <string>
#include <iostream>
#include <type_traits>

#define X1 pp("x")
#define X2 pp("x", "x")

void pp(const std::string str)
{
   std::cout << str << std::endl;
}

int main()
{
   std::map<std::string, int> map;

   if constexpr (std::is_null_pointer_v<decltype(map)>)
      X2;
   else
      X1;
}
Run Code Online (Sandbox Code Playgroud)

并吐出此错误消息:

#include <map>
#include <string>
#include <iostream>
#include <type_traits>

#define X1 pp("x")
#define X2 pp("x", "x")

void pp(const std::string str)
{
   std::cout << str << std::endl;
}

int main()
{
   std::map<std::string, int> map; …
Run Code Online (Sandbox Code Playgroud)

c++ templates if-statement constexpr c++17

30
推荐指数
3
解决办法
3339
查看次数

为什么`std :: string :: find()`在失败时不返回结束迭代器?

我发现的行为std::string::find与标准C ++容器不一致。

例如

std::map<int, int> myMap = {{1, 2}};
auto it = myMap.find(10);  // it == myMap.end()
Run Code Online (Sandbox Code Playgroud)

但是对于一串,

std::string myStr = "hello";
auto it = myStr.find('!');  // it == std::string::npos
Run Code Online (Sandbox Code Playgroud)

为什么不应该失败的myStr.find('!')回报myStr.end(),而不是std::string::npos

由于std::string与其他容器相比,它有些特殊,所以我想知道这背后是否有真正的原因。(令人惊讶的是,我找不到任何人在任何地方对此进行质疑)。

c++ stdstring c++-standard-library

29
推荐指数
1
解决办法
777
查看次数

如何创建一个类似于“范围”的浮动对象?

我想range创建一个类似-的构造,它将像这样使用:

for (auto i: range(5,9))
    cout << i << ' ';    // prints 5 6 7 8 

for (auto i: range(5.1,9.2))
    cout << i << ' ';    // prints 5.1 6.1 7.1 8.1 9.1
Run Code Online (Sandbox Code Playgroud)

处理整数情况相对容易:

template<typename T>
struct range 
{
    T from, to;
    range(T from, T to) : from(from), to(to) {}

    struct iterator
    {
        T current;
        T operator*() {  return current; }

        iterator& operator++()
        {
            ++current;
            return *this;
        }

        bool operator==(const iterator& other) { return …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point templates iterator c++17

24
推荐指数
4
解决办法
1824
查看次数

如何比较两个函数的签名?

有没有办法检查两个函数是否具有相同的签名?例如:

int funA (int a, int b);
int funB (int a, int b);
float funC (int a, int b);
int funD (float a, int b);
Run Code Online (Sandbox Code Playgroud)

在此示例中,funAand funB是应该返回的功能的唯一组合true

c++ function c++-standard-library function-signature c++17

22
推荐指数
3
解决办法
974
查看次数

如何调用模板类型的正确构造函数?

在下面的代码中,如何使注释行的工作方式与其正上方的行相同?

我想让它成为一个通用代码,调用模板的合适构造函数Type

#include <string>
#include <iostream>

template <typename Type>
struct Class
{
    Type data;
    Class(Type data) : data(data) { }
};

int main()
{
    Class<std::string> a = std::string("abc");
    // Class<std::string> b = "abc";
    std::cout << a.data << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates initialization class c++11

21
推荐指数
3
解决办法
633
查看次数