这是一个相关帖子的续集,它询问了永恒的问题:
我可以在C++中使用具有值语义的多态容器吗?
问题稍有不正确.应该更像是:
我可以使用按值存储的基类型的STL容器,其中元素表现出多态行为吗?
如果你用C++问这个问题,答案是"不".在某些时候,您将切片按值存储的对象.
现在我再次提出这个问题,但严格来说是C++ 11.通过对语言和标准库的更改,现在可以在STL容器中按值存储多态对象吗?
我很清楚在容器中存储智能指针到基类的可能性 - 这不是我正在寻找的,因为我试图在不使用的情况下在堆栈上构造对象new.
考虑你是否(从链接的帖子)作为基本的C++示例:
#include <iostream>
using namespace std;
class Parent
{
public:
Parent() : parent_mem(1) {}
virtual void write() { cout << "Parent: " << parent_mem << endl; }
int parent_mem;
};
class Child : public Parent
{
public:
Child() : child_mem(2) { parent_mem = 2; }
void write() { cout << "Child: " << parent_mem << ", " << child_mem << endl; }
int child_mem;
}; …Run Code Online (Sandbox Code Playgroud) 我似乎无法使我的正则表达式正常工作.在ECMAScript中的多行文本中,这个正则表达式begin\n([\s\S]*\nend)?完全符合我的需要,我在这里测试了它.
当我将它翻译成C++时,它无法匹配相同的文本.
这是我在Visual C++ 2010中的代码:
#include <iostream>
#include <regex>
int main(int argc, char *argv[]) {
std::regex metadataBlockRegex("begin\\n([\\s\\S]*\\nend)?",
std::regex::ECMAScript);
std::string text =
"begin\n"
" 123\n"
"end\n";
std::sregex_iterator blocksBegin(text.begin(), text.end(), metadataBlockRegex);
std::sregex_iterator blocksEnd;
for (auto blockMatch = blocksBegin; blockMatch != blocksEnd; ++blockMatch) {
std::cout << (*blockMatch)[0].str();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这只输出"开始",我希望它匹配整个文本.
我的问题是:这里有什么问题,我在哪里可以找到std::regex引擎语法的详细描述以及它们如何处理多行字符串.
我有一个对象作为接口的引用/指针.如果存在该方法,我想在具体对象上调用一个方法,而不需要更改接口,破坏封装或编写任何可怕的黑客攻击.怎么做到呢?
这是一个例子.
我有一个界面:
class IChatty
{
public:
virtual ~IChatty() {};
virtual std::string Speak() const = 0;
};
Run Code Online (Sandbox Code Playgroud)
并且这个界面的多个具体实现:
class SimpleChatty : public IChatty
{
public:
~SimpleChatty() {};
virtual std::string Speak() const override
{
return "hello";
}
};
class SuperChatty : public IChatty
{
public:
void AddToDictionary(const std::string& word)
{
words_.insert(word);
}
virtual std::string Speak() const override
{
std::string ret;
for(auto w = words_.begin(); w != words_.end(); ++w )
{
ret += *w;
ret += " ";
}
return ret;
} …Run Code Online (Sandbox Code Playgroud) 序言: 众所周知,将指针放在数组末尾之后是合法且明确定义的:
int main()
{
int na [1] = {};
const int* naBegin = na;
const int* naEnd = na + 1; // one-past-end, OK
}
Run Code Online (Sandbox Code Playgroud)
此指针可用于比较,这有助于C样式数组(或更准确地说,其中的指针)与采用迭代器的标准库例程兼容,例如copy(Live Demo):
template <typename Field, typename Iter>
void foo(Iter begin, Iter end)
{
std::copy (begin, end, std::ostream_iterator <Field> (std::cout, std::endl);
}
int main()
{
int na [1] = {};
foo <int> (na, na + 1);
}
Run Code Online (Sandbox Code Playgroud)
标准(C++ 03参考)支持其合法性和定义性:
5 /当一个具有整数类型的表达式被添加到指针或从指针中减去时,结果具有指针操作数的类型.如果指针操作数指向数组对象的元素,并且数组足够大,则结果指向偏离原始元素的元素,使得结果元素和原始数组元素的下标的差异等于整数表达式.换句话说,如果表达式P指向数组对象的第i个元素,则表达式(P)+ N(等效地,N +(P))和(P)-N(其中N具有值n)指向分别为数组对象的第i + n和第i-n个元素,只要它们存在.此外,如果表达式P指向数组对象的最后一个元素,则表达式(P)+1指向一个超过数组对象的最后一个元素,如果表达式Q指向一个超过数组对象的最后一个元素,表达式(Q)-1指向数组对象的最后一个元素.如果指针操作数和结果都指向同一个数组对象的元素,或者指向数组对象的最后一个元素,则评估不应产生溢出; 否则,行为未定义.
当我在标准中查看对过去指针的浊度的引用时,我发现的每个引用都在讨论数组.如果我们试图将对象的地址过去,而不是数组呢?
问题: …
我正在开发一个简单的静态 HTML 网站。现在,只有一个 html 文件,index.html。使用 Webpack 3.10.0。我已经配置了 HMR。
当我对其中一个样式表进行更改时,它会按预期重新编译和重新加载:
[WDS] App updated. Recompiling...
client?5cf9:222 [WDS] App hot update...
log.js:23 [HMR] Checking for updates on the server...
log.js:23 [HMR] Updated modules:
log.js:15 [HMR] - ./src/styles/app.scss
log.js:23 [HMR] App is up to date.
Run Code Online (Sandbox Code Playgroud)
但是当我对index.html进行更改时,chrome控制台说它正在重新编译,但在重新编译结束时它说“没有热更新”:
[WDS] App updated. Recompiling...
client?5cf9:222 [WDS] App hot update...
log.js:23 [HMR] Checking for updates on the server...
log.js:23 [HMR] Nothing hot updated.
log.js:23 [HMR] App is up to date.
Run Code Online (Sandbox Code Playgroud)
我使用单独的 dev 和 prod webpack 配置文件,并将通用的配置文件合并到每个配置文件中。常见的是这样的:
const path = …Run Code Online (Sandbox Code Playgroud) 几天前,我正在研究 istream 迭代器和异常处理,我对此感到好奇:
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
if (argc < 2) {
cout << argv[0] << " <file>" << endl;
return -1;
}
try {
ifstream ifs(argv[1]);
ifs.exceptions(ios::failbit | ios::badbit);
istream_iterator<string> iss(ifs), iss_end;
copy(iss, iss_end, ostream_iterator<string>(cout, "\n"));
}
catch (const ios_base::failure& e) {
cerr << e.what() << endl;
return -2;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么在读取输入文件的最后一个字后总是引发失败位异常?
请考虑以下代码:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
string myAry[] =
{
"Mary",
"had",
"a",
"Little",
"Lamb"
};
const size_t numStrs = sizeof(myStr)/sizeof(myAry[0]);
vector<string> myVec(&myAry[0], &myAry[numStrs]);
copy( myVec.begin(), myVec.end(), ostream_iterator<string>(cout, " "));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里感兴趣的是&myAry[numStrs]:numStrs等于5,所以&myAry[numStrs]指向不存在的东西; 数组中的第六个元素.在上面的代码中有另一个例子: myVec.end()它指向向量的一个结尾myVec.获取不存在的此元素的地址是完全合法的.我们知道它的大小string,所以我们知道C型数组的string第6个元素的地址必须指向何处.只要我们只评估这个指针并且从不取消引用它,我们就没事了.我们甚至可以将它与其他指向相等的指针进行比较.STL一直在处理一系列迭代器的算法中执行此操作.该end()迭代器指向过去的结束,以及循环,同时保持一个计数器循环!= end().
所以现在考虑一下:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int …Run Code Online (Sandbox Code Playgroud) MSVC 10和MSVC 9在编译我的异常框架时都会生成4级警告消息,尽管程序的行为似乎是正确的.异常框架相当庞大和复杂,但我已经设法将其归结为其本质.这是一个可以在VS10中编译和运行的完整程序
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
namespace ex
{
class generic_error : virtual public std::exception
{
public:
generic_error(int thread_id) : thread_id_(thread_id) {}
const char* what() const throw()
{
static std::string msg;
stringstream ss;
ss << "generic error in thread #" << thread_id_;
msg = ss.str();
return msg.c_str();
}
int thread_id_;
};
template<class EX>
class traced_error : virtual public std::exception, virtual public EX
{
public:
traced_error(int line, const EX& ex): EX(ex), …Run Code Online (Sandbox Code Playgroud) 我尝试在C中运行以下程序并获得一些输出.你能帮帮我吗?
#include<stdio.h>
int main()
{
char x='A';
printf("%d%d%d",sizeof("3"),sizeof('3'),sizeof(3));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在ubuntu 11.04 32位中使用gcc收到的输出为2 4 4.
同样在其他计划中: -
#include<stdio.h>
int main()
{
char x='A';
printf("%d%d",sizeof('A'),sizeof(x));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在ubuntu 11.04 32位中使用GCC接收的输出为4 1.
你能帮我解决为什么输出是这样的吗?
我按照我以 tessesract 形式找到的有关如何包含 baseapi.h 的说明进行操作。
我在用:
vs2010
tesseract 3.01 版本
我试图了解如何使用 baseapi.h。
测试程序:
#define __MSW32__
#include "baseapi.h"
using namespace tesseract;
int _tmain(int argc, _TCHAR* argv[])
{
TessBaseAPI *myTestApi;
myTestApi=new TessBaseAPI();
//myTestApi->Init("d:/temp.jpg","eng");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
表格指南:
将以下文件夹添加到其他包含目录(属性) - 解决包含“baseapi.h”后找不到文件的问题
tesseract-3.01/api
tesseract-3.01/ccmain
tesseract-3.01/ccutil
tesseract-3.01/ccstruct

将以下库添加到“属性/链接器/输入/附加依赖项”以使用 Tesseract 和 Leptonica 库 libtesseract.lib;liblept.lib
// 将以下路径添加到“Properties/Linker/General/Additional Library Directories”以找到 Tesseract 和 Leptonica 库 tesseract-3.01/vs2010/Release tesseract-3.01/vs2008/lib

我现在试着跑

所以我尝试找到 libs libtesseract.lib 并替换为 libtesseract_tessopt.lib 然后运行

1>------ Build started: Project: test4, Configuration: Debug Win32 ------
1> test4.cpp
1>test4.obj : error LNK2019: …Run Code Online (Sandbox Code Playgroud)