我有一个std::vector<int>和第二个容器持有迭代器或索引(没有键,我希望不断访问元素)到这个向量用于删除目的.让我们假设我有一个1000个元素的向量,并想要删除其中的200个元素.在删除操作之后,未删除元素的顺序应该与之前相同.
我在问题的第一个版本中错过了另一件事:价值观是独一无二的.他们是身份.
你如何在安全(关于stl规则)和有效方式(传统的决定是最终的)中做到这一点?
我想到的可能性或方法:
vector.erase(vector.begin()+index+offset)同时删除元素,同时保持在容器中删除索引以计算偏移量.可以使用std::lower_bound已经移除的元素的容器来确定每次移除迭代的该偏移.问题:由于随机位置删除,很多binary_searches用于获取偏移量和大量移动操作.vector.erase.现在我没有使任何迭代器失效,除了删除本身之外没有向量重新排列操作.问题:很多排序那么,你会如何解决这个问题呢?有什么新想法吗?有什么建议?
感谢您的输入.
萨沙
编辑/更新/拥有结果:我实现了擦除 - 删除习惯用法,这也是KennyTM提到的,带有一个基于boost :: dynamic_bitset中的查找的谓词,并且它的速度非常快.此外,我尝试了PigBen的move-and-truncate方法(也由Steve Jessop提到),它也在它的while循环中访问bitset.对我的数据来说,两者似乎同样快.我试图删除100个1000个元素(无符号整数)中的100个,这100个删除了1M次并且没有显着差异.因为我认为基于stl的擦除删除成语更"天生",我选择了这种方法(KennyTM也提到了参数).
这里的目标是让我的完整数据库配置和模式生成由流畅的nhibernate处理.
有没有办法用Fluent nhibernate指定多列索引?
我知道您可以在单个属性/列上指定索引
像这样:
mapping.Map(x => x.ItemDt).Index("IX_DataTransferLog_ItemDt");
Run Code Online (Sandbox Code Playgroud)
如何指定多列?
如果这不可能,有没有办法将原始SQL语句添加到将在创建/更新架构后运行的配置?
来自维基百科css的示例:
#content a[href^="https://"], .link-https {
background:url("images/external-link-ltr-icon.png?2") no-repeat scroll right center transparent;
padding:0 13px 0 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Lift框架反序列化JSON文本,并且它们似乎不支持Seq特征(尽管支持List).举个例子...
一些代表员工的JSON数据(名字和姓氏)......
{"employees":[{"fname":"Bob","lname":"Hope"},{"fname":"Bob","lname":"Smith"}]}
Run Code Online (Sandbox Code Playgroud)
这是员工域对象:
case class Employee(fname: String, lname: String) { }
case class Employees(employees: Seq[Employee]) { }
Run Code Online (Sandbox Code Playgroud)
这是我的JSON反序列化代码......
class EmployeeTest {
@Test def test() {
val jsonText: String = ....
val e = deserialize(jsonText)
}
def deserialize(in: String): Employees = {
implicit val formats = net.liftweb.json.DefaultFormats
net.liftweb.json.Serialization.read[Employees](in)
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将Employees域对象更改为使用List而不是Seq,那么它可以工作.但是如果可以的话,我真的很想使用Seq.
这是我在运行上面的代码时看到的异常(使用Seq):我能做些什么来让它工作吗?谢谢你的帮助!
net.liftweb.json.MappingException: unknown error
at net.liftweb.json.Extraction$.extract(Extraction.scala:43)
at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:288)
at net.liftweb.json.Serialization$.read(Serialization.scala:50)
at EmployeeTest.deserialize(EmployeeTest.scala:20)
at EmployeeTest.test(EmployeeTest.scala:13)
Caused by: java.lang.UnsupportedOperationException: tail of empty list
at scala.collection.immutable.Nil$.tail(List.scala:388)
at scala.collection.immutable.Nil$.tail(List.scala:383)
at net.liftweb.json.Meta$Constructor.bestMatching(Meta.scala:60)
at net.liftweb.json.Extraction$.findBestConstructor$1(Extraction.scala:187) …Run Code Online (Sandbox Code Playgroud) 我在Django和Google App Engine上使用python.我也在我的一个模型中使用DateTimeProperty.偶尔我想向用户显示该日期和时间.
将DateTimeProperty中存储的日期时间转换为用户日期时间的最佳方法是什么?
或者更精确地构建问题的方法:获取客户端时区并将python datetime对象转换为自己的本地时间的最佳方法是什么?
我已经掌握了LINQ-to-SQL的基础知识,但我一直在努力让JOIN正常工作.我想知道如何将以下内容转换为LINQ-to-SQL(理想情况下使用方法链接,因为这是我的首选格式).
SELECT c.CompanyId, c.CompanyName,
p.FirstName + ' ' + p.LastName as AccountCoordinator,
p2.FirstName + ' ' + p2.LastName as AccountManager
FROM dbo.Companies c
INNER JOIN dbo.Persons p
ON c.AccountCoordinatorPersonId = p.PersonId
INNER JOIN dbo.Persons p2
ON c.AccountManagerPersonId = p2.PersonId
Run Code Online (Sandbox Code Playgroud) 这个问题很容易.在Web应用程序的客户端使自动完成很漂亮很简单.有很多插件.
但是,在服务器端的背面,最好的方法是什么?我不喜欢用用户按下每个按键来击中数据库的想法.
我一直在考虑sphinx,或者从你的网站并行运行的一些全文搜索引擎.
例如,如果我有一个PHP(高流量)网站,我可以创建一个并行python脚本,从我的"自动完成文本框"获取http请求.然后,当用户按下客户端的密钥时,AJAX请求将被定向到可以使用特殊策略的python脚本.
什么是你的方法?
一些惯例:
database optimization performance full-text-search autocomplete
如果我像这样使用它,可以用于多个事件吗?
unit Unit4;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Clipbrd;
type
TForm4 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure WMHotkey(var Message: TWMHotKey); message WM_HOTKEY;
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form4: TForm4;
implementation
const
MY_ID = 123;
MY_ID1 = 123;
MY_ID2 = 123;
{$R *.dfm}
procedure TForm4.FormCreate(Sender: TObject);
begin
RegisterHotKey(Handle, MY_ID, MOD_CONTROL, ord('1'));
RegisterHotKey(Handle, MY_ID1, MOD_CONTROL, ord('2'));
RegisterHotKey(Handle, MY_ID2, MOD_CONTROL, ord('3'));
end;
procedure TForm4.FormDestroy(Sender: TObject);
begin
UnregisterHotKey(Handle, …Run Code Online (Sandbox Code Playgroud) std::vector,std::list并且std::deque有std::back_inserter和std::set有std::inserter.
对于std::stack和std::priority_queue我会假设等效的插入器将是一个,push()但我似乎无法找到正确的函数来调用.
我的意图是能够使用以下函数和正确的插入迭代器:
#include <string>
#include <queue>
#include <iterator>
template<typename outiter>
void foo(outiter oitr)
{
static const std::string s1 ("abcdefghji");
static const std::string s2 ("1234567890");
*oitr++ = s1;
*oitr++ = s2;
}
int main()
{
std::priority_queue<std::string> spq;
std::stack<std::string> stk;
foo(std::inserter(spq));
foo(std::inserter(stk));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这工作正常(从初始测试).
由于方法链接是我的首选格式,我试图弄清楚链接等效方法是什么,但没有运气.有任何想法吗?
var data = (from p in db.Persons
from c in db.Companies
where c.CompanyName == companyName && p.CompanyId == c.CompanyId
select p)
.Select(p => new
{
Id = p.PersonId,
Name = string.Format("{0} {1}", p.FirstName, p.LastName)
});
Run Code Online (Sandbox Code Playgroud)
谢谢, -
埃德
c# ×2
c++ ×2
linq ×2
linq-to-sql ×2
performance ×2
stl ×2
algorithm ×1
autocomplete ×1
containers ×1
css ×1
database ×1
delphi ×1
django ×1
events ×1
indexing ×1
inner-join ×1
inserter ×1
json ×1
lambda ×1
lift ×1
list ×1
localization ×1
nhibernate ×1
optimization ×1
python ×1
scala ×1
seq ×1
shortcut ×1
std ×1
taction ×1