#include <boost/regex.hpp>
#include <string>
#include <iostream>
using namespace boost;
static const regex regexp(
"std::vector<"
"(std::map<"
"(std::pair<((\\w+)(::)?)+, (\\w+)>,?)+"
">,?)+"
">");
std::string errorMsg =
"std::vector<"
"std::map<"
"std::pair<Test::Test, int>,"
"std::pair<Test::Test, int>,"
"std::pair<Test::Test, int>"
">,"
"std::map<"
"std::pair<Test::Test, int>,"
"std::pair<Test::Test, int>,"
"std::pair<Test::Test, int>"
">"
">";
int main()
{
smatch result;
if(regex_match(errorMsg, result, regexp))
{
for (unsigned i = 0; i < result.size(); ++i)
{
std::cout << result[i] << std::endl;
}
}
// std::cout << errorMsg << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会产生:
terminate called after throwing …
Run Code Online (Sandbox Code Playgroud) 我想弄清楚我抛出的对象存储在内存中的位置.所以我为它写了一个小程序:
#include <iostream>
#define print_dist() int a;\
do { std::cout << __FUNCTION__ << "() a[" << (long)&a - (long)ptrMainStackBase << "]" << std::endl; } while (0)
#define print_distx(x) \
do { std::cout << __FUNCTION__ << "() " #x "[" << (long)&x - (long)ptrMainStackBase << "]" << std::endl; } while (0)
#define print_distxy(x, y) \
do { std::cout << __FUNCTION__ << "() " #x "(ex)[" << (long)&x - (long)y << "]" << std::endl; } while (0)
class CTest
{
public:
CTest() …
Run Code Online (Sandbox Code Playgroud) 我有一个包含许多表的程序,所以我想在调试程序时在日志文件中跟踪它们.我想要彩色的格式化输出,所以我想我是用HTML格式编写的.那么用C++创建HTML文件的最佳方法是什么?
琐碎的方式是如此恶心和容易出错:
std::stringstream ret;
ret << "<TABLE BORDER=\"1\" CELLBORDER=\"0\">\n";
ret << " <TR>\n";
...
Run Code Online (Sandbox Code Playgroud)
所以我认为我写了一个HTML包装器,但我认为有这么多原因.
我希望在C++中使用这种语法或类似的静态类型HTML包装器:
CHtmlHtml Html;
Html <<
Body() % Id("1")
<< H1("My First Heading") <<
<< P("My first paragraph.");
Run Code Online (Sandbox Code Playgroud)
或者在这种情况下
CHtmlTable table;
table % Border(1) % CellBorder(0) <<
Tr() <<
Td("Text") % Colspan(4);
Run Code Online (Sandbox Code Playgroud)
有没有类似的项目?
那么递增或递减end()迭代器是在标准中定义的吗?在linux上,begin()实现为end()++.
#include <list>
#include <iostream>
int main()
{
std::list<int> numbers;
for (int i = 0; i < 10; i++)
numbers.push_back(i);
auto it = numbers.begin();
int count = 3;
while (count)
{
std::cout << *it++;
if (it == numbers.end())
{
++it; // is this ok ???
--count;
std::cout << '\n';
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么每个平台的输出总是一样的吗?
输出:
0123456789
0123456789
0123456789
Run Code Online (Sandbox Code Playgroud) 这是一个简单的测试用例,无需任何警告即可编译.看起来像一个常见的错误,但clang,gcc和visual studio在这种情况下不会发出警告.为什么?
class Image {
private:
int width, height;
int* array;
public:
Image(int _width, int _height);
void crashTest();
};
Image::Image(int _width, int _height)
{
array = new int[width * height];
// ^^^^^ ^^^^^^ this is wrong
// I expect a warning here e.g.: 'width is uninitialized here'
width = _width;
height = _height;
}
void Image::crashTest()
{
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
array[x + y * …
Run Code Online (Sandbox Code Playgroud) 在我们公司,我们拥有非常强大的基于Linux的构建服务器(具有40核的双Xeon)和不那么强大的win7笔记本电脑.我们用C/C++语言构建我们的产品,用于深奥的CPU.编译器仅存在于Linux中.我可以用Qt Creator编辑我的git repo.它工作得很快,一切都很快.但我无法在笔记本电脑上构建源代码.我们有一个主要的git repo,我可以将相同的repo克隆到我的笔记本电脑和构建服务器.当我按下构建按钮时,我希望实现这一点,我的代码在构建服务器上神奇地构建.我做了一个概念证明解决方案,其中我的构建脚本在我的repo上执行git diff并将其scp到构建服务器,而不是ssh构建服务器在服务器repo上应用diff而不是启动并等待编译.但是这个解决方案并不是那么简单.我认为存在更好的接近/方法.那么如何在外部服务器上构建我的git repo?
我有一个带有一些成员的结构,我有一个实现的运算符==.在operator ==的帮助下实现运算符是否安全?我想在一个集合中使用这个结构,我想检查这个结构是否是唯一的.
struct Data
{
std::string str1;
std::string str2;
std::string str3;
std::string str4;
bool operator==(const Data& rhs)
{
if (str1 == rhs.str1
&& str2 == rhs.str2
&& str3 == rhs.str3
&& str4 == rhs.str4
)
return true;
else
return false;
}
// Is this ok??
bool operator<(const Data& rhs)
{
return !this->operator==(rhs);
}
}
Run Code Online (Sandbox Code Playgroud)
所以当我将这个结构插入到std :: set时会发生什么?
我想为仅从第二次迭代运行的 for 循环创建一个辅助函数,因为我有很多具有以下模式的代码:
firstItem = true;
for (unsigned i = 0; i < 5; ++i)
{
firstItem ? firstItem = false : std::cout << ",\n";
std::cout << i;
}
Run Code Online (Sandbox Code Playgroud)
我开始考虑一个辅助函数,并提出了这个解决方案:
template <typename T>
void fromSecondIter(T fn)
{
static bool firstItem = true; // because of this it works with unique types only
if (firstItem)
firstItem = false;
else
fn();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当 T 是唯一类型时,此解决方案可以正常工作。因此传递 lambda 是可以的,但传递函数会默默地导致错误。然后我将 fn 参数包装到 lambda 中,但令人惊讶的是它没有帮助;
这是一个完整的示例:
#include <type_traits>
#include <iostream>
#include <functional>
template <typename T>
void …
Run Code Online (Sandbox Code Playgroud) 这段代码有什么问题,如何解决?
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>
struct CTest
{
CTest()
{ std::cout << "ctor CTest" <<std::endl; }
~CTest()
{ std::cout << "dtor CTest" <<std::endl; }
};
struct CSlot
{
CSlot() : m_test(new CTest()), m_num(123)
{ }
~CSlot()
{
// m_test.reset(); // this line fixed the code but I don't know why
m_num = -1;
}
boost::shared_ptr<CTest> m_test;
int m_num;
};
int main()
{
std::vector<CSlot> testVector(1);
std::cout << "1" << std::endl;
new (&testVector[0]) CSlot();
// clear slot
testVector[0].~CSlot();
std::cout << …
Run Code Online (Sandbox Code Playgroud) 所以我想使用llvm::Twine
字符串块类.
我有以下样本:
#include <llvm/ADT/Twine.h>
#include <iostream>
int main()
{
llvm::Twine twine1 = llvm::Twine("aaaa") + "bbbb" + "cccc" + "dddd";
llvm::Twine twine2 = llvm::Twine(twine1) + "dddd" + "eeee";
std::cout << twine1.str() << std::endl;
std::cout << twine2.str() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它运行与clang++
使用-O3
,并g++
与-O0
但段错误g++
使用-O3
.我尝试了这个代码从3.4-3.9部分不同版本的clang库并试过g++ 4.8.4
,g++ 4.8.5
和mingw-5.3.0
.
您需要llvm库并将代码与-lLLVMSupport -lLLVMCore
其他代码链接起来llvm-config --ldflags
c++ ×10
c++11 ×2
algorithm ×1
boost ×1
boost-regex ×1
clang ×1
compilation ×1
constructor ×1
destructor ×1
exception ×1
git ×1
html ×1
html-helper ×1
iterator ×1
lambda ×1
list ×1
llvm ×1
regex ×1
set ×1
shared-ptr ×1
std ×1
templates ×1
valgrind ×1
wrapper ×1