在我的代码中,我有这样的事情:
unordered_multimap<string, unordered_map<string, string> > mEntities;
...
vector<unordered_map<string, string> > rawEntities;
if (qi::phrase_parse(&buf[0], (&buf[0]) + buf.size(), EntityParser<char*>(), qi::space, rawEntities)) {
for (auto &propGroup : rawEntities) {
auto search = propGroup.find("classname");
if (search != propGroup.end()) {
// is stealing propGroup safe???
mEntities.emplace(search->second, std::move(propGroup));
}
}
}
// rawEntities goes out of scope here
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我正在使用std::move推导出的类型的对象unordered_map<string, string>&,这显然不是unordered_map<string, string>&&.尽管如此,我确信因为rawEntities在for循环之后超出范围,其元素(字符串 - >字符串映射)将永远不会再次使用.所以我认为窃取(移动)其元素数据是安全的,因为它们不会被再次使用.
当我运行程序时,它似乎工作.但这是不好的做法/反模式,特别是,标准是否保证它是安全的?
您可以在K&R的The C Programming Language中找到此代码:
void *malloc(unsigned nbytes) {
Header *p, *prevp;
Header *moreroce(unsigned);
unsigned nunits;
nunits = (nbytes+sizeof(Header)-1)/sizeof(header) + 1;
if ((prevp = freep) == NULL) { /* no free list yet */
base.s.ptr = freeptr = prevptr = &base;
base.s.size = 0;
}
for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
if (p->s.size >= nunits) { /* big enough */
if (p->s.size == nunits) { /* exactly */
prevp->s.ptr = p->s.ptr;
} else …Run Code Online (Sandbox Code Playgroud) 我有一个使用Apache Spark的Java程序.该程序中最有趣的部分如下所示:
long seed = System.nanoTime();
JavaRDD<AnnotatedDocument> annotated = documents
.mapPartitionsWithIndex(new InitialAnnotater(seed), true);
annotated.cache();
for (int iter = 0; iter < 2000; iter++) {
GlobalCounts counts = annotated
.mapPartitions(new GlobalCounter())
.reduce((a, b) -> a.sum(b)); // update overall counts (*)
seed = System.nanoTime();
// copy overall counts which CountChanger uses to compute a stochastic thing (**)
annotated = annotated
.mapPartitionsWithIndex(new CountChanger(counts, seed), true);
annotated.cache();
// adding these lines causes constant time complexity like i want
//List<AnnotatedDocument> ll = annotated.collect();
//annotated = …Run Code Online (Sandbox Code Playgroud) 我正在尝试计算两个角度间隔之间的交点,如下图所示。不幸的是,-pi 处的分支使代码比我希望的要丑陋得多。这是我的初稿。请注意,我尚未测试此代码的正确性,而只是在我的脑海中浏览了这些场景。
正如您在函数 中看到的branchify,角度间隔受到限制,从(p)a1 -> (p)a2逆时针方向看,差异最多为 pi。否则,间隔由最小角度差定义。[a1, a2]是第一个间隔,[pa1, pa2]第二个。
// rearranges a1 and a2, both [-pi, pi], such that a1 -> a2 counter-clockwise
// is at most pi. Returns whether this interval crosses the branch.
static inline bool branchify(float &a1, float &a2) {
if (abs(a1-a2) >= 1.5707963267948966192313216916398f) {
if (a1 < a2) swap(a1, a2);
return true;
} else {
if (a1 > a2) swap(a1, a2);
return false;
}
}
float …Run Code Online (Sandbox Code Playgroud) 我有使用问题JTextArea.我的实际设置不同,但效果仍然存在.这是问题的图像:

拥有的JDialog大小仅比JTextArea其首选大小所需的大约1个像素的那一刻,文本区域突然调整大小.在我的实际设置中,它们突然长高.我正在使用a GridBagLayout,但它似乎发生在其他布局中.为什么是这样?
以下是重现上述效果的易于编译的代码.
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class TextDemo extends JDialog implements ActionListener {
private static final long serialVersionUID = -589374238138963529L;
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";
private static final java.awt.Dimension SCREENSIZE =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
private static final java.awt.Point SCREENCENTER =
new java.awt.Point(SCREENSIZE.width/2,SCREENSIZE.height/2);
public TextDemo(Window owner, String shortMessage, String message, JComponent accessory) {
super(owner);
setTitle("Test");
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
Icon …Run Code Online (Sandbox Code Playgroud) 这是我的mman.h文件,包括/usr/include/sys/:
mmap()似乎以某种方式定义了(并且可以与标志 0 和读/写保护一起使用),但不是MAP_ANON 或 MAP_ANONYMOUS如下所示:
#include <sys/mman.h>
int a = MAP_ANON; /* compile error */
int b = MAP_ANONYMOUS; /* also compile error */
Run Code Online (Sandbox Code Playgroud)
我不知所措。我正在使用这个 makefile 进行编译:
编辑:原来MAP_ANONYMOUS宏定义在/usr/include/bits/mman.h但只有当__USE_MISC定义...
任何帮助都会很棒!
让我们说我想要的语义是
class Foo {
public:
Foo(A &x);
Foo(A &x, A &y);
void method();
private:
A &x, &y;
};
Foo::Foo(A &x) : x(x) {}
Foo::Foo(A &x, A &y) : x(x), y(y) {}
void Foo::method() {
if (first_constructor_used) {
} else {
}
}
Run Code Online (Sandbox Code Playgroud)
现在很明显我无法检查成员变量x和yNULL.添加类似check int变量的内容以查看运行的构造函数似乎很笨重而且不可扩展.有没有人对这里应该使用的语义有什么建议?
在我的C++ 03代码中,我有很多看起来像这样的函数:
class A {
public:
template <Iterator>
void doSomethingWithObjects(Iterator begin, Iterator end) {
for (Iterator point = begin; point != end; point++) {
mInternal.doSomething(*point);
}
}
private:
DataStructure mInternal;
};
Run Code Online (Sandbox Code Playgroud)
我试图在新代码中尽可能多地使用C++ 11的功能,特别是基于范围的for循环.我的问题是,如何使用模板化迭代器执行此操作?是否有一个神奇的C++结构,它采用两个模板化的迭代器类型,并将它们转换为范围表达式?换句话说,我正在寻找这样的东西:
class A {
public:
template <Iterator>
void doSomethingWithObjects(Iterator begin, Iterator end) {
static_assert(std::is_same<Point, typename std::decay<Iterator>::type>::value, "wrong type mate!"); // extra credit
for (auto&& point : std::magic(begin, end)) {
mInternal.doSomething(point);
}
}
private:
DataStructure mInternal;
};
Run Code Online (Sandbox Code Playgroud)
如果在C++ 11中有一种新的,首选的方法来做这种"为这个结构添加一些对象",我也很满意.
我只涉足正则表达式,想知道是否有人可以帮助我制作一个 Java 正则表达式,它与具有以下特性的字符串相匹配:
它应该匹配
但不是
我尝试过以下正则表达式字符串
[a-zA-Z^\\-_]+[\\-_]?[a-zA-Z^\\-_]*
Run Code Online (Sandbox Code Playgroud)
这似乎有效。但是,我不确定如何用这种方法来完成总字符限制部分。我也尝试过
[[a-zA-Z]+[\\-_]?[a-zA-Z]*]{1,14}
Run Code Online (Sandbox Code Playgroud)
但它匹配(例如) abc-cde_aa ,但它不应该匹配。