计算字符串中所有子字符串出现次数的最佳方法是什么?
示例:计算Foo内部的出现次数FooBarFooBarFoo
请问#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) 我的目标是尝试解决这个问题:从 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) 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.但它没有用.
我有以下详细代码:
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,因此不会丢失任何功能(即 …
为什么第二次调用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) 为什么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 ++ 17及std::filesystem::last_write_time了,但现在我还需要的文件的创建时间,似乎没有成为在一个API std::filesystem。我错过了吗?如果没有,怎么没有呢?
我是否必须使用不可移植的代码来提取该创建时间戳?是否助推。文件系统的始祖std::filesystem,有这样的API吗?
通常,我们可以使用动态分配的大括号初始化来创建一个数组
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,但希望是否有其他方法。
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)
我对这两个案例感到困惑。它们完全一样吗?