小编JeJ*_*eJo的帖子

计算字符串中字符串的出现次数

计算字符串中所有子字符串出现次数的最佳方法是什么?

示例:计算Foo内部的出现次数FooBarFooBarFoo

c++ string counting

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

#ifdef _DEBUG在主要功能中

请问#ifdef _DEBUG在主要功能有任何意义,如果我在工作的Visual Studio 2013

如果是,那是为了什么?

int _tmain(int argc, _TCHAR* argv[])
{
#ifdef _DEBUG

//creating some objects, using  functions etc;

#endif
}
Run Code Online (Sandbox Code Playgroud)

c++ macros visual-studio visual-studio-debugging

5
推荐指数
1
解决办法
2431
查看次数

使用带闭包的 lambda 回调

我正在尝试实现一个回调,该回调将控制从中断服务例程传递到 c++ 类上的成员函数。我认为lambdas和闭包将是一种方便的方法,但我在实现它时遇到了麻烦。下面是我的代码的简化版本。

我遇到的问题是如何将“函数指针”存储到“ lambda ”。

class Gpio
{
  public:
    typedef void (*ExtiHandler)();    
  private:
    ExtiHandler handler;    
  public:
    void enable_irq(ExtiHandler handler_in) 
    { 
      // enable interrupt
      // ...
      // save handler so callback can be issued later
      handler = handler_in;
    }
};

class Button
{
  private:
    Gpio& pin;    
  public:
    Button(Gpio& pin_in) : pin(pin_in) 
    {
    };

    void button_pressed()
    {
      // do something
    }

    void init()
    {
      pin.enable_irq([this]() { this->button_pressed(); });
    }
};
Run Code Online (Sandbox Code Playgroud)

编译失败并显示以下错误消息;

 no matching function for call to …
Run Code Online (Sandbox Code Playgroud)

c++ lambda closures function-pointers c++11

5
推荐指数
2
解决办法
1714
查看次数

C++:查找满足谓词的元组的第一个元素

我有以下详细代码:

struct thing1 { int key, std::string value; };
struct thing2 { int key, std::string value; };
// ...
struct thingN { int key, std::string value; };

struct thing_map {
  thing1 t1;
  thing2 t2;
  // ...
  thingN tN;

  std::string get(int key) {
    if(t1.key == key) return t1.value;
    if(t2.key == key) return t2.value;
    // ...
    if(tN.key == key) return tN.value;
    throw std::runtime_error("bad key");
  }
};
Run Code Online (Sandbox Code Playgroud)

我可以将things 重构为 an std::tuple<thing1, thing2, /* ... */ thingN>,这允许我使用 typed 访问它们std::get,因此不会丢失任何功能(即 …

c++ templates stdtuple c++17

5
推荐指数
1
解决办法
869
查看次数

如何判断一个对象是否有堆分配成员?

有没有一种方法可以确定一个对象在堆上是否至少有一个成员?

我试图能够将像std::stringor std::vectoror这样的对象std::list(是的,主要是容器)与所有其他类型区分开来(不幸的是,即使是具有单个元素的容器也在我的“感兴趣范围”内)

我正在尝试做类似以下的事情:

struct foo
{
private:
    int * _ptr;

public:
    foo() : _ptr(new int) {};
    ~foo() { delete _ptr; };
};

struct bar
{
private:
    int val;
};

template <typename T>
void func(T val)
{
    if constexpr (std::is_class_v<T>)
    {
        std::cout << std::setw(20) << typeid(T).name() << " is a class type." << std::endl;
        if (/* determine if foo has any heap allocations */)
        {
            // Do something #1.
            std::cout << std::setw(20) << typeid(T).name() << …
Run Code Online (Sandbox Code Playgroud)

c++ templates class member c++17

5
推荐指数
1
解决办法
901
查看次数

我无法通过参考捕获传递lambda

以下代码因此错误而失败

E0413不存在从“ lambda [] float(int i)-> float”到“ float(*)(int i)”的合适转换函数

int test;   
float (*f)(int i) = [&](int i) -> float {return test; };
Run Code Online (Sandbox Code Playgroud)

我该如何解决?我需要Capture子句。

c++ lambda function-pointers function c++11

5
推荐指数
1
解决办法
127
查看次数

为什么按值传递的数组上的 std::size 不起作用?

为什么std::size()不能在按值传递的静态分配数组上工作?

void print_elemTab(int tab[])
{
   // ...
   int size = std::size(tab); //error 
   // ...
}

void test_tab()
{
   const int    TAB_SIZE = 5;
   int          tab[TAB_SIZE] = {};
   // ...
   cout << std::size(tab) << std::endl; //print 5
   print_elemTab(tab);
   // ...
}
Run Code Online (Sandbox Code Playgroud)

我正在打印尺寸,然后在我再次使用tab的子功能print_elemTab()中传递std::size()

没有得到匹配的函数错误,所以我想知道为什么std::size()第一次在test_tab()而不是在print_elemTab()

我必须通过引用传递它吗?那么我如何制作它,但对于任意长度的数组呢?

还是因为我不知道的事情我必须以另一种方式制作它?

c++ arrays pass-by-reference c++17

5
推荐指数
1
解决办法
180
查看次数

为什么不能迭代直接通过 std::Optional&lt;T&gt;::value() 访问的容器?

我正在尝试迭代通过 a 访问的 astd::vector<X>中包含的a 。与我的预期相反,如果我首先将其存储到副本中与直接迭代它,则行为会有所不同:struct Tstd::optional<T>std::optional<T>

#include <optional>
#include <string>
#include <vector>

// The rest is the same in both implementations    
struct Object
{
    std::string id;
    std::vector<SomeStructType> items;
};

Object readDataFile() { /*....*/ return {}; }
bool dataFileExists() { /*....*/ return true; }

std::optional<Object> getData()
{
    if (!dataFileExists()) {
        return std::nullopt;
    }

    Object obj = readDataFile();
    return obj;
}

int main()
{
    // Implementation 1 - works
    auto items = getData().value().items;
    for (auto const& item …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm c++20 stdoptional range-based-loop

5
推荐指数
1
解决办法
116
查看次数

按字母顺序对结构数组进行排序

我有一个名为Sheep的结构数组(A [#])(因为我的任务是关于绵羊DNR).在我做任何要求的任务后,我留下了这个结构:

struct Sheep 
{
    string Vardas;
    char Fragmentas[CMax]; 
    int atitikme = 0; 
};
Run Code Online (Sandbox Code Playgroud)

在我的内部,数据是:

(string Vardas) | (char Fragmentas[CMax]) | (int atitikme)
Baltukas   TAGCTT 3
Bailioji   ATGCAA 3 
Smarkuolis AATGAA 1 
Run Code Online (Sandbox Code Playgroud)

(char Fragmentas [CMax]将不会使用,所以你不必看它,我只是将它命名为明确).

所有这些数据都来自U2.txt文件,无法在代码中手动输入.

它剩下要做的就是按照这些规则对它进行排序:

  1. 它通过'int atitikme'从大到小.
  2. 如果'int atitikme'相等,那么它必须按'A [#]排序.Vardas按字母顺序排列.

要通过'int atitikme'对其进行排序,我创建了一个代码:

string q;
char w[20];
int e;
for (int o = 0; o < n-1; o++) 
{
    for (int p = o+1; p < n-1; p++) 
    {
        if (A[p].atitikme > A[o].atitikme) 
        {
            // - Vardo Keitimas
            q …
Run Code Online (Sandbox Code Playgroud)

c++ sorting algorithm stdvector c++11

4
推荐指数
1
解决办法
337
查看次数

为双向迭代器提供 operator+ 或 operator- 是否有任何缺点?

双向迭代器有没有像奢侈品随机访问迭代器,因此需要依赖于std::nextstd::prev,当有人需要做的操作,如

std::set<int> s{ 1, 2, 3, 4, 5 };

//std::set<int> s2(s.cbegin(), s.cbegin() + 2); // won't work as there is no operator+ for std::set::const_iterator
std::set<int> s2(s.cbegin(), std::next(s.cbegin(), 2));

//std::set<int> s3(s.cbegin(), s.cend() - 2);  // won't work as there is no operator- for std::set::const_iterator
std::set<int> s3(s.cbegin(), std::prev(s.cend(), 2));
Run Code Online (Sandbox Code Playgroud)

但是,我们可以实现这些 operator+operator-使用上面的std::nextstd::prev.

#include <set>
#include <iterator>  // std::iterator_traits, std::next, std::prev

template<typename InputIt>
constexpr InputIt operator+(InputIt it,
    typename …
Run Code Online (Sandbox Code Playgroud)

c++ iterator operator-overloading c++-standard-library language-lawyer

4
推荐指数
1
解决办法
121
查看次数