我有一个 C 风格的数组(不一定以 null 结尾)。我想用正则表达式搜索它。我的代码如下:
const void* Search(const char* startAddress, const char* endAddress, std::regex *re)
{
std::smatch match;
auto ret = std::regex_search(startAddress, endAddress, *re);
Run Code Online (Sandbox Code Playgroud)
在目前的形式下,它工作得很好,但是我想知道它在哪里找到了那个特定的模式。一旦我将匹配添加为参数,编译器就无法找到合适的重载函数。我尝试在该区域之外创建一个string_view
,但编译器也找不到任何适合这些迭代器的重载。
我正在专门寻找std::regex
解决方案。我应该如何使用它?
Visual Studio 2017,带有C ++ 17的C ++
有什么方法可以使用模板参数并推断基本类型?
示例:如果template参数是std::string
我想char
在函数内部声明一个。如果template参数是std::basic_string<unsigned char>
我想unsigned char
在函数内部声明:
#include <string>
#include <vector>
#include <typeinfo>
using namespace std;
typedef unsigned char uchar;
typedef basic_string<uchar> ustring;
template <typename str>
void exampleFunc(vector<str> &vec)
{
// Pseudo-code to be evaluated at ***compile time***
if (typeof(str) == ustring)
uchar c;
if (typeof(str) == string)
char c;
// ... code ...
}
int main()
{
vector<string> vec;
vector<ustring> uvec;
exampleFunc(vec);
exampleFunc(uvec);
}
Run Code Online (Sandbox Code Playgroud) 在 C++ 中工作时,这更像是一个首选的样式问题。我正在寻找这方面的首选标准。
假设我有一个接口 A
class AInterface {
public:
virtual ~AInterface() = default;
virtual void a() = 0;
};
Run Code Online (Sandbox Code Playgroud)
然后我有另一个接口 B 建立在 A 之上
class BInterface : public AInterface{
public:
virtual void b() = 0;
virtual void otherb() = 0;
};
Run Code Online (Sandbox Code Playgroud)
我的问题是我应该像这样重新声明 B 中 AInterface 的所有虚函数吗?
class BInterface : public AInterface{
public:
virtual void a() = 0;
virtual void b() = 0;
virtual void otherb() = 0;
};
Run Code Online (Sandbox Code Playgroud) c++ inheritance abstract-class virtual-functions pure-virtual
我在 Visual Studio Code 中运行 C++ 代码时遇到错误,我无法在编辑器中运行任何代码,这里是我多次搜索此问题但没有得到任何结果的错误图像。我尝试使用“|” 代替 '&&' 但仍然没有任何改进。我的 VS Code 是最新的。有人愿意分享一些解决这个问题的方法吗?似乎“#include <bits/stdc++.h>”文件中有错误,因为它说第 1 行和 char 46、char 51 等有错误...
任何有关此的信息将不胜感激,感谢您的宝贵时间...
我正在学习 C++ 。在“Type Fields
我的书”主题下,声明如下:
为了在声明中使用派生类不仅仅是一种方便的简写,我们必须解决以下问题:给定一个类型的指针,所指向
Base*
的对象真正属于哪个派生类型?这个问题有四种基本的解决方案:
- 确保只指向单一类型的对象。
- 在基类中放置一个类型字段,供要检查的函数使用。
- 用
dynamic_cast
- 使用虚函数
我的问题 - 真正的类型字段是什么,段落试图解决的问题是什么?
我问这个是因为关于 SO 的其他相关问题似乎是针对旧版本的 C++ 标准,没有提到任何形式的并行化,或者专注于在删除元素时保持排序/索引相同。
我有一个可能包含数十万或数百万个元素的向量(它们是相当轻的结构,假设它们被压缩了大约 20 个字节)。
由于其他限制,它必须是 astd::vector
并且其他容器不起作用(例如std::forward_list
),或者在其他用途中甚至不太理想。
我最近从简单的it = std::erase(it)
方法切换到使用 pop-and-swap 使用这样的东西:
for(int i = 0; i < myVec.size();) {
// Do calculations to determine if element must be removed
// ...
// Remove if needed
if(elementMustBeRemoved) {
myVec[i] = myVec.back();
myVec.pop_back();
} else {
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,并且是一个显着的改进。它将方法的运行时间减少到以前的 61%。但我想进一步改进这一点。
C++ 是否有一种方法可以std::vector
有效地从 a 中删除许多非连续元素?就像将索引向量传递给erase()
C++ 并让 C++ 在幕后做一些魔术以最大程度地减少数据移动?
如果是这样,我可以让线程单独收集必须并行删除的索引,然后组合它们并将它们传递给擦除()。
C++ 理论上,如果我创建了一个名为Box
. 我已经完成了所有变量和服务等。如果我被要求创建一个程序,我需要在其中创建一个动态数组的指针类变量。
为什么我需要使用 ex: Box **boxes= nullptr;
?
另外,如果我被问到除了声明 Box 名称框之外,我还需要声明一个等于 0 的整数变量大小:
int size = 0;
Run Code Online (Sandbox Code Playgroud)
如何void addBox(Box** box, int &size)
完成一个被调用的函数,以便每次调用它时我都会添加 box 的参数,并将另一个 box 添加到动态数组中?
仅供参考,我是编码新手,我需要将这个概念应用到课堂上的项目中。
不应该std::unique_ptr
防止出现这种错误的可能性吗?
#include <iostream>
#include <vector>
#include <memory>
struct B {
int b;
};
int main()
{
std::vector<std::unique_ptr<B>> v; // unique_ptr can be stored in a container
B* p = new B;
v.emplace_back(p);
std::cout << "p:" <<p <<"\n";
std::cout << "v[0]:"<<v[0].get() << "\n";
v.emplace_back(p);
std::cout << "p:" <<p <<"\n";
std::cout << "v[1]:"<<v[1].get() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
检测到双重释放时的错误信息:
*** Error in `./a.out': double free or corruption (fasttop): 0x0000000001094c20 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f3da200b7e5]
...
======= Memory map: ========
00400000-00402000 r-xp 00000000 …
Run Code Online (Sandbox Code Playgroud) 在 C++98 中,以下代码无法编译,因为 ifstream 没有复制构造函数:
#include <iostream>
#include <fstream>
using namespace std;
ifstream f() {
return ifstream("main.cpp");
}
int main() {
ifstream st= f();
}
Run Code Online (Sandbox Code Playgroud)
但是,在 C++11 中使用多个 GCC 版本时,编译时不会发出警告。这是什么原因?
c++ ×9
c++17 ×2
pointers ×2
c++11 ×1
c++20 ×1
class ×1
fstream ×1
inheritance ×1
integer ×1
powershell ×1
pure-virtual ×1
regex ×1
standards ×1
stdvector ×1
templates ×1
unique-ptr ×1
xvalue ×1