好吧,我希望我在这里犯了一个愚蠢的错误.我有一个DisplayDevice3d列表,每个DisplayDevice3d都包含一个DisplayMode3d列表.我想删除DisplayDevice3d列表中没有任何DisplayMode3d的所有项目.我正在尝试使用Lambda来做它,即:
// If the device doesn't have any modes, remove it.
std::remove_if(MyDisplayDevices.begin(), MyDisplayDevices.end(),
[](DisplayDevice3d& device)
{
return device.Modes.size() == 0;
}
);
Run Code Online (Sandbox Code Playgroud)
即使出6名DisplayMode3d在MyDisplayDevices,只有1个有什么DisplayMode3d的其模式集合中,没有被从列表中删除.
我在这里犯了什么错误?
编辑:
好吧,我的错误是我应该使用MyDisplayDevices.remove_if而不是std :: remove_if,但是下面的答案对于使用std :: remove_if:p是正确的.
MyDisplayDevices.remove_if( [](DisplayDevice3d const & device)
{
return device.Modes.size() == 0;
});
Run Code Online (Sandbox Code Playgroud) 我想从std::unordered_map
(直方图)中删除元素(直方图箱),这些元素(直方图)满足作为lambda表达式给出的预测(直方图箱具有零计数),如下所示
std::remove_if(begin(m_map), end(m_map), [](const Bin & bin) { return bin.second == 0; });
Run Code Online (Sandbox Code Playgroud)
但GCC-4.6.1抱怨如下
/usr/include/c++/4.6/bits/stl_pair.h:156:2: error: assignment of read-only member ‘std::pair<const unsigned char, unsigned char>::first’
/usr/include/c++/4.6/bits/stl_pair.h: In member function ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_T1, _T2>&&) [with _T1 = const unsigned char, _T2 = long unsigned int, std::pair<_T1, _T2> = std::pair<const unsigned char, long unsigned int>]’:
/usr/include/c++/4.6/bits/stl_algo.h:1149:13: instantiated from ‘_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = std::__detail::_Hashtable_iterator<std::pair<const unsigned char, long unsigned int>, false, false>, _Predicate = pnw::histogram<V, C, H>::pack() [with …
Run Code Online (Sandbox Code Playgroud) 我有这个:
Post.paragraphs << new_paragraph
Run Code Online (Sandbox Code Playgroud)
我需要通过id = 3删除段落,因此以下内容将完全删除记录:
Post.paragraphs.find(paragraph_id).destroy
# or
Post.paragraphs.find(paragraph_id).delete
Run Code Online (Sandbox Code Playgroud)
我只需要从has_many关联中删除一个段落.我试着用delete
和destroy
.两种方法都完全删除关联表中的记录.我怎样才能将它们从"容器"中删除?
在另一个主题中,我试图解决这个问题.问题是从a中删除重复的字符std::string
.
std::string s= "saaangeetha";
Run Code Online (Sandbox Code Playgroud)
由于订单并不重要,所以我s
先排序,然后使用std::unique
并最终调整大小以获得所需的结果:
aeghnst
Run Code Online (Sandbox Code Playgroud)
那是正确的!
现在我想做同样的事,但同时我希望字符的顺序完好无损.意思是,我想要这个输出:
sangeth
Run Code Online (Sandbox Code Playgroud)
所以我写了这个:
template<typename T>
struct is_repeated
{
std::set<T> unique;
bool operator()(T c) { return !unique.insert(c).second; }
};
int main() {
std::string s= "saaangeetha";
s.erase(std::remove_if(s.begin(), s.end(), is_repeated<char>()), s.end());
std::cout << s ;
}
Run Code Online (Sandbox Code Playgroud)
这给出了这个输出:
saangeth
Run Code Online (Sandbox Code Playgroud)
也就是说,a
重复,但其他重复已经消失.代码有什么问题?
无论如何我改变了我的代码 :(见评论)
template<typename T>
struct is_repeated
{
std::set<T> & unique; //made reference!
is_repeated(std::set<T> &s) : unique(s) {} //added line!
bool …
Run Code Online (Sandbox Code Playgroud) 我有一个正则表达式来验证字符串.但现在我想删除所有与我的正则表达式不匹配的字符.
例如
regExpression = @"^([\w\'\-\+])"
text = "This is a sample text with some invalid characters -+%&()=?";
//Remove characters that do not match regExp.
result = "This is a sample text with some invalid characters -+";
Run Code Online (Sandbox Code Playgroud)
有关如何使用RegExpression确定有效字符并删除所有其他字符的任何想法.
非常感谢
我最近问了一个关于从矢量中删除项目的问题.好吧,我得到的解决方案,但我不明白 - 我找不到任何解释它的文档.
struct RemoveBlockedHost {
RemoveBlockedHost(const std::string& s): blockedHost(s) {}
// right here, I can find no documentation on overloading the () operator
bool operator () (HostEntry& entry) {
return entry.getHost() == blockedHost || entry.getHost() == "www." + blockedHost;
}
const std::string& blockedHost;
};
Run Code Online (Sandbox Code Playgroud)
用作:
hosts.erase(std::remove_if(hosts.begin(), hosts.end(), RemoveBlockedHost(blockedhost)), hosts.end());
Run Code Online (Sandbox Code Playgroud)
我查看了std :: remove_if的文档,它说只有在类重载()运算符时才可以传递一个类而不是一个函数.没有任何信息.
有谁知道链接:
对此的帮助将不胜感激.我不喜欢在我的软件中添加代码,除非我理解它.我知道它有效,而且我(稍微)熟悉运算符重载,但我不知道()运算符是什么.
可能重复:
remove_if等效于std :: map
我有一组字符串:
set <wstring> strings;
// ...
Run Code Online (Sandbox Code Playgroud)
我希望根据谓词删除字符串,例如:
std::remove_if ( strings.begin(), strings.end(), []( const wstring &s ) -> bool { return s == L"matching"; });
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我得到以下编译器错误:
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(1840): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>'
Run Code Online (Sandbox Code Playgroud)
该错误似乎表明std::string
没有按值复制构造函数(这将是非法的).是它在某种程度上不好用std::remove_if
用std::set
?我应该做其他事情,比如几次迭代set::find()
后跟set::erase()
吗?
filter
在Emacs Lisp中是否有相当于高阶函数的东西?喜欢python或Javascript的功能.
(filter-equivalent (lambda (n) (= (% n 2) 0)) '(1 2 3 4 5 6 7 8))
==> (2 4 6 8)
Run Code Online (Sandbox Code Playgroud) 在一个函数中,我需要在尝试删除html并添加新内容之前检查(div,span,p)是否包含任何.html元素.
不知道怎么做...
我尝试了这个,但没有工作:
// HERE below I tried to do a check to see if the div's have HTML, but did not work
if ($('.'+rowName+' div').html) {
$('.'+rowName+' div').html.remove();
$('.'+rowName+' span').html.remove();
$('.'+rowName+' p').html.remove();
}
Run Code Online (Sandbox Code Playgroud)
功能齐全
// Create the Role / Fan rows
function rowMaker (rowName, roleName) {
//alert(rowName+' '+roleName);
// HERE below I tried to do a check to see if the div's have HTML, but did not work
if ($('.'+rowName+' div').html) {
$('.'+rowName+' div').html.remove();
$('.'+rowName+' span').html.remove();
$('.'+rowName+' p').html.remove();
} …
Run Code Online (Sandbox Code Playgroud) 在我的应用程序有不同的4个链路ID
秒和4 DIV
具有相同ID
的各个环节(我使用他们的锚跳跃).
我目前的代码:
<a href="#1" id="go1" class="btn" data-anchor="relativeanchor">One</a>
<a href="#2" id="go2" class="btn" data-anchor="relativeanchor">Two</a>
<a href="#3" id="go3" class="btn" data-anchor="relativeanchor">Three</a>
<a href="#4" id="go4" class="btn" data-anchor="relativeanchor">Four</a>
<div class="col-md-12 each-img" id="1">
<img src="img/album-img.png">
</div>
<div class="col-md-12 each-img" id="2">
<img src="img/album-img.png">
</div>
<div class="col-md-12 each-img" id="3">
<img src="img/album-img.png">
</div>
<div class="col-md-12 each-img" id="4">
<img src="img/album-img.png">
</div>
Run Code Online (Sandbox Code Playgroud)
有时用户id="2"
在点击按钮之前只先滚动到第二个div ,当他们这样做时,他们会先发送到顶部id="1"
而不是继续下一个ID
id="3"
.
使用时,一次只能看到一个按钮CSS
,当单击链接时,我删除该链接.
CSS
a.btn{display: none}
a.btn a:first-child{display: block !important;}
Run Code Online (Sandbox Code Playgroud)
jQuery的
$(document).ready(function(){
$('a.btn').click(function …
Run Code Online (Sandbox Code Playgroud)