我正在维护一组unique_ptr实例priority_queue.在某些时候,我想获得第一个元素并将其从队列中删除.但是,这总是会产生编译器错误.请参阅下面的示例代码
int main ()
{
std::priority_queue<std::unique_ptr<int>> queue;
queue.push(std::unique_ptr<int>(new int(42)));
std::unique_ptr<int> myInt = std::move(queue.top());
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下编译器错误(gcc 4.8.0):
uptrtest.cpp: In function ‘int main()’: uptrtest.cpp:6:53: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]’ std::unique_ptr<int> myInt = std::move(queue.top());
^ In file included from /usr/include/c++/4.8/memory:81:0,
from uptrtest.cpp:1: /usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here
unique_ptr(const unique_ptr&) = delete;
^
Run Code Online (Sandbox Code Playgroud)
更改要queue在此问题中使用的代码可以解决问题并且代码编译得很好.
有没有办法保持unique_ptrs priority_queue或我错过了什么?
所以我写了这段代码,这一行use(private lang: string): Promise<object>给了我一个错误“A parameter property in only allowed in constructor implementation”
当我删除访问修饰符时它会起作用,private但我只是好奇为什么它会给我这个错误以及正确的方法是什么?
@Injectable()
export class TranslateService {
public data: object = {};
constructor(private http: HttpClient) {}
use(private lang: string): Promise<object> {
return new Promise<object>((resolve, reject) => {
const langPath = `assets/i18n/${lang || 'en'}.json`;
this.http.get<object>(langPath).subscribe(
translation => {
this.data = Object.assign({}, translation || {});
resolve(this.data);
},
error => {
this.data = {};
resolve(this.data);
}
);
});
}
}
Run Code Online (Sandbox Code Playgroud) 我有一组指向对象的唯一指针.有时,我会透露一些指向这些对象的原始指针,因此代码的其他部分可以对这些对象进行处理.此代码不知道指针是否指向由某组唯一指针维护的对象,因此我需要检查指针指向的对象是否在唯一的指针集中.
在简单的代码中:
int* x = new int(42);
std::set<std::unique_ptr<int>> numbers;
numbers.insert(std::unique_ptr<int>(x));
numbers.find(x) // does not compile
Run Code Online (Sandbox Code Playgroud)
我理解为什么代码不能编译,但我想不出用STL搜索元素的方法.有什么能满足我的需求,还是我必须手动迭代集合的所有元素?
我正在尝试使用以下代码从文件中读取数据。(请注意,您需要在 GCC 上启用 C++11 功能才能进行此编译。)
#include <fstream>
typedef unsigned char byte;
int main()
{
std::string filename = "test.cpp";
std::basic_ifstream<byte> in(filename, std::basic_ifstream<byte>::in | std::basic_ifstream<byte>::binary);
in.exceptions(std::ios::failbit | std::ios::badbit);
byte buf[5];
in.read(buf, 5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,在读取数据时出现异常:
抛出“std::bad_cast”实例后调用终止 what(): std::bad_cast
in.read(buf, 5)调用命令时会发生这种情况。
我知道我可以通过不设置我设置的异常掩码来抑制这个异常,但这并不能解决问题,它只会掩盖它。没有异常掩码,代码继续工作,但读取了 0 个字符。
有谁知道为什么会抛出这个异常?我该如何让它消失?
在Symfony 2.7之前,字段的attr值choice仅应用于字段本身,即<select>呈现的元素.我用它来为这个元素应用类来设置它的样式.
在Symfony 2.7中,此行为已更改.现在,元素的所有<option>子<select>元素也获得相同的属性(提交更改),因此也获得类.
有些澄清,请将其作为代码:
<?php echo $view['form']->widget($form['myField'], ['attr' => ['class' => "text ui-widget-content ui-corner-all"]]); ?>
Run Code Online (Sandbox Code Playgroud)
然后这是Symfony <= 2.6的输出:
<select class="text ui-widget-content ui-corner-all" name="myField">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
Run Code Online (Sandbox Code Playgroud)
这是Symfony> = 2.7的输出:
<select class="text ui-widget-content ui-corner-all" name="myField">
<option value="1" class="text ui-widget-content ui-corner-all">Option 1</option>
<option value="2" class="text ui-widget-content ui-corner-all">Option 2</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我申请的类不适合<option>元素,因为它们定义了实际字段的边框等.请注意,这些是由jQuery UI定义的类,因此我无法轻松更改其定义.
避免将这些类应用于字段的所有<option>元素choice同时仍将其应用于元素的最简单方法是什么<select>?
TL; DR
使用可变维度键实现字典过滤功能的最有效方法是什么?过滤器应采用与字典键相同尺寸的元组,并输出字典中与过滤器匹配的所有键,以便filter[i] is None or filter[i] == key[i]适用于所有维度i.
在我目前的项目中,我需要处理包含大量数据的字典.字典的一般结构是这样的,它包含2到4个整数作为键和整数作为值的元组.字典中的所有键具有相同的尺寸.为了说明,以下是我需要处理的字典示例:
{(1, 2): 1, (1, 5): 2}
{(1, 5, 3): 2}
{(5, 2, 5, 2): 8}
Run Code Online (Sandbox Code Playgroud)
这些词典包含大量条目,其中最大的条目大约有20 000个条目.我经常需要过滤这些条目,但通常只查看关键元组的某些索引.理想情况下,我想要一个我可以提供过滤器元组的功能.然后该函数应返回与过滤器元组匹配的所有键.如果过滤器元组包含一个None条目,那么这将匹配该索引处字典的关键元组中的任何值.
函数应该对具有二维键的字典执行的操作的示例:
>>> dict = {(1, 2): 1, (1, 5): 2, (2, 5): 1, (3, 9): 5}
>>> my_filter_fn((1, None))
{(1, 2), (1, 5)}
>>> my_filter_fn((None, 5))
{(1, 5), (2, 5)}
>>> my_filter_fn((2, 4))
set()
>>> my_filter_fn((None, None))
{(1, 2), (1, 5), (2, 5), (3, 9)}
Run Code Online (Sandbox Code Playgroud)
由于我的词典具有不同的元组维度,我尝试通过编写生成器表达式来解决这个问题,该表达式考虑了元组的维度:
def …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过KIT 编译Contraction Hierarchies Implementation.
该软件已于2009年发布,从那以后显然没有得到维护.由于一些事情在平均时间内发生了变化(使用新的C++标准和编译器版本),代码不再编译.
在编写指令时,README并不是非常冗长,并且说你应该只调用它make.但是,使用make会给我以下错误:
g++ -Wall -W -Wno-unused-parameter -O6 -c -o main.o main.cpp
In file included from /usr/include/c++/4.8/ext/hash_map:60:0,
from command/../io/../datastr/graph/../../io/serialize.h:26,
from command/../io/../datastr/graph/edge.h:26,
from command/../io/../datastr/graph/graph.h:59,
from command/../io/createGraph.h:28,
from command/NodeOrder.h:29,
from main.cpp:35:
/usr/include/c++/4.8/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file … 几周前我刚开始尝试使用C++.在尝试使用C++之前,我对Java有了相当不错的把握.很多人告诉我他们在语法方面非常相似.
在底部有一个switch语句启动战斗场景.每当我选择战斗选项时,它就会关闭程序.
这是我的代码:
#include "stdafx.h"
#include <iostream>
#include <cstdlib> // For rand()
#include <string>
#include <sstream>
#include <algorithm> // transform()
#include <cctype> // toupper(), tolower()
#include <functional> // ptr_fun()
#include <time.h>
// PUT S*** BELOW THIS POINT
//____________________________________________________________________________
using namespace std;
int main()
{
/* Expirimental text based adventure game.
* Mainly being used for practice methods.
* Developed by Zack Cook.
Generic title. Bad story. Bad interactions.
Lots and lots of bad, bad code. Be warned.
*/
string charName; …Run Code Online (Sandbox Code Playgroud) 有人告诉我,vector.reserve()在插入大量元素之前调用是个好习惯.我刚刚遇到一种情况,我想将大量const元素放入其中vector.reserve()但是,在调用时,会抛出编译器错误.
请考虑以下代码重现问题:
#include <vector>
int main()
{
std::vector<const int> vec;
vec.reserve(2);
}
Run Code Online (Sandbox Code Playgroud)
这会导致以下巨大的编译器错误:
In file included from /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++allocator.h:33:0,
from /usr/include/c++/4.8/bits/allocator.h:46,
from /usr/include/c++/4.8/vector:61,
from vecreserve.cpp:1:
/usr/include/c++/4.8/ext/new_allocator.h: In instantiation of ‘struct __gnu_cxx::new_allocator’:
/usr/include/c++/4.8/bits/allocator.h:92:11: required from ‘class std::allocator’
/usr/include/c++/4.8/bits/alloc_traits.h:90:43: required from ‘struct std::allocator_traits >’
/usr/include/c++/4.8/ext/alloc_traits.h:121:10: required from ‘struct __gnu_cxx::__alloc_traits >’
/usr/include/c++/4.8/bits/stl_vector.h:75:28: required from ‘struct std::_Vector_base >’
/usr/include/c++/4.8/bits/stl_vector.h:210:11: required from ‘class std::vector’
vecreserve.cpp:5:26: required from here
/usr/include/c++/4.8/ext/new_allocator.h:93:7: error: ‘const _Tp* __gnu_cxx::new_allocator::address(__gnu_cxx::new_allocator::const_reference) const [with _Tp = const … 我使用TortoiseHg作为版本控制解决方案。因为我将每个功能放在单独的分支上,所以我经常创建新的分支。每次我要推送新分支时,都会向我发出有关在远程存储库中创建新分支的警告:
我对此有些恼火,因为当我在该分支上推送变更集时,无法想到不在远程存储库中创建分支的原因。有什么办法可以告诉TortoiseHg始终将新分支推送到远程存储库?
c++ ×6
c++11 ×3
unique-ptr ×2
angular ×1
dictionary ×1
file-io ×1
filtering ×1
forms ×1
g++ ×1
gcc ×1
gcc4.8 ×1
ifstream ×1
io ×1
mercurial ×1
performance ×1
php ×1
python ×1
set ×1
symfony ×1
symfony-2.7 ×1
tortoisehg ×1
typescript ×1
vector ×1