小编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
查看次数

为什么擦除删除习语不适用于反向迭代器

我的目标是尝试解决这个问题:从 end 中删除向量中的所有空元素。使用擦除删除成语。

这个想法是在给定std::vector<std::string>的字符串a中删除从末尾开始的所有元素,这些元素是空的(等于空白)。当发现非空元素时,应停止删除元素。

例子:

vec = { " ", "B", " ", "D", "E", " ", " ", " " };
Run Code Online (Sandbox Code Playgroud)

移除后:

vec = { " ", "B", " ", "D", "E"};
Run Code Online (Sandbox Code Playgroud)

这是我尝试的解决方案:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>

int main()
{
    std::vector<std::string> vec = { " ", "B", " ", "D", "E", " ", " ", " " };

    bool notStop = true;
    auto removeSpaceFromLast = [&](const std::string& …
Run Code Online (Sandbox Code Playgroud)

c++ base stdvector erase-remove-idiom stl-algorithm

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

如何使此函数使用getline读取字符串并使用int执行相同的操作?

template<typename T>
T get(const string &prompt)
{
    cout<<prompt;
    T ret;
    cin>>ret;
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何通过重载来做到这一点; 基本上,这适用于任何类型的数据,对......

我尝试了typeid(variable).name();并获得了一个字符串变量的输出,并尝试在get函数中创建一个if.但它没有用.

c++ string templates overloading getline

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

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 :: list而不是父类的std :: list传递给函数?

为什么第二次调用print_all函数会导致静态语义错误

#include <list>
using std::list;
class foo {
    // ...
};
class bar : public foo {
    // ...
};

static void print_all(list<foo*>& L) {
    // ...
}

list<foo*> LF;
list<bar*> LB;
// ...
print_all(LF); // works fine 
print_all(LB); // static semantic error
Run Code Online (Sandbox Code Playgroud)

c++ inheritance class function stdlist

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

为什么按值传递的数组上的 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::filesystem::last_write_time() 等价物在哪里?

我使用std::filesystem::last_write_time了,但现在我还需要的文件的创建时间,似乎没有成为在一个API std::filesystem。我错过了吗?如果没有,怎么没有呢?

我是否必须使用不可移植的代码来提取该创建时间戳?是否助推。文件系统的始祖std::filesystem,有这样的API吗?

c++ filesystems file c++-standard-library c++17

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

是否可以使用 std::make_unique 对动态数组进行大括号初始化?

通常,我们可以使用动态分配的大括号初始化来创建一个数组

int* arr = new int[5]{1,1,2,4,5};
Run Code Online (Sandbox Code Playgroud)

但这可以使用智能指针,特别是使用 std::make_unique 吗?我尝试过以下方法:

unique_ptr<int[]> arr(5) {1,1,2,4,5};
unique_ptr<int[]> arr = make_unique<int[]>(5){1,1,2,4,5};
unique_ptr<int[]> arr = make_unique<int[]>({1,1,2,4,5});
Run Code Online (Sandbox Code Playgroud)

但没有用,而且我认为使用智能指针甚至不可能实现这一点。任何有关如何对智能指针使用大括号初始化的建议将不胜感激。

是的,我知道std::vector,但希望是否有其他方法。

c++ initialization smart-pointers unique-ptr c++11

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

移动 std::map 与移动 std::map 的所有元素

std::map<int, Obj> mp;
// insert elements into mp

// case 1
std::map<int, Obj> mp2;
mp2 = std::move(mp);

// case 2
std::map<int, Obj> mp3;
std::move(std::begin(mp), std::end(mp), std::inserter(mp3, std::end(mp3));
Run Code Online (Sandbox Code Playgroud)

我对这两个案例感到困惑。它们完全一样吗?

c++ move stdmap std c++11

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