我正在努力找出为什么我无法转换为使用模板类.
这是模板类的简化版本:
template<typename T>
class base
{
public :
base() : all_() {}
~base() {}
public:
bool add(T t)
{
typename vector<T>::iterator itr
= lower_bound(all_.begin(), all_.end(), t);
if ( itr == all_.end() || *itr != t )
{
all_.push_back(t);
cout << "ok" << endl;
return true;
}
cout << "failed" << endl;
return false;
}
static bool addTo(base<T> *c, T t)
{
return c->add(t);
}
private :
vector<T> all_;
};
Run Code Online (Sandbox Code Playgroud)
这是我尝试使用transform来捕获add成员函数的所有bool输出的地方:
main()
{
base<int> test;
vector<bool> results;
vector<int> toAdd;
toAdd.push_back(10); …Run Code Online (Sandbox Code Playgroud) 我发现一篇文章说,删除矢量项的最佳方法是使用擦除 - 删除习语,如下所示
//say vec = 1963,1923,1900,1963,1967;
vector.erase(std::remove(vec.begin(),vec.end(),1963),vec.end());
Run Code Online (Sandbox Code Playgroud)
根据我的理解,vec.erase在第一个和最后一个迭代器中包含两个参数.范围包括第一个和最后一个之间的所有元素.我想知道如果要删除的值存在于向量中的不同索引处,std :: remove将如何给出单个迭代器范围.我想知道std :: remove如何与vector.erase一起使用
我必须实现一个函数,以字典顺序在控制台上打印每个字符串,作为第一个字母,char c,仅使用stl算法.
这是我的想法:
void f(const std::vector<std::string>& vs, const char c)
{
std::vector<std::string> tmp = vs;
std::sort(tmp.begin(), tmp.end());
std::ostream_iterator<std::string> out(std::cout, "\n");
std::copy_if(tmp.begin(), tmp.end(), out, *predicate*);
}
Run Code Online (Sandbox Code Playgroud)
作为谓词,我想:
//*(tmp.begin()->begin()) == c);
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我在Stack Overflow和其他论坛上找到了这个解决方案,用于从字符串中删除字符.假设我想从字符串中删除空格:
currentLine.erase( std::remove( currentLine.begin(), currentLine.end(), ' ' ), currentLine.end() );
Run Code Online (Sandbox Code Playgroud)
其中currentLine是字符串的名称.
这种事似乎适用于人,但如果我使用它,我得到:
/local/yrq12edu/Desktop/Bens_C++_Utilities/simuPOPtoFASTA/simuPOP2FASTA.cpp|54|error: cannot convert 'std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}' to 'const char*' for argument '1' to 'int remove(const char*)'|
Run Code Online (Sandbox Code Playgroud)
作为编译错误.我认为它与std :: remove函数返回的迭代器有关,不能使用erase方法,但显然它应该可以工作.我该如何解决?
std::map<std::string, Obj> myMap;
std::set<std::string> mySet;
Run Code Online (Sandbox Code Playgroud)
我想删除那些myMap没有键的对mySet.
我该怎么做?我找到了std::remove_if算法,但似乎不适用于此.
std::vector<std::thread> thread_pool;
...
std::generate_n(std::back_inserter(thread_pool), cpu_cores,
[] (){
//...
return std::thread{worker(worker_name) };
} );
Run Code Online (Sandbox Code Playgroud)
哪里:
class worker {
std::atomic_bool done;
protected:
void operator()() {
while(!done) {
// some work
}
}
public:
worker(const std::string& worker_name)
: done(false) {
// some initialization
}
// other fields
};
error: use of deleted function 'std::atomic_bool::atomic_bool(const std::atomic_bool&)'
Run Code Online (Sandbox Code Playgroud)
GCC 4.9
因为我看到原子无法复制,只是感动了.上面的代码需要复制some_object类的ctor.怎么解决这个?
(可能设计本身更糟糕,这里,some_object是一个线程的仿函数,而atomic是一个关闭进程的标志)
我想从地图中找到第一个非零元素,因此我做了以下代码:
#include <map>
#include <iostream>
#include <algorithm>
bool nonzero(std::map<char,int>::const_iterator& it);
int main() {
std::map<char, int> m;
m['a'] = 0;
m['b'] = 1;
std::map<char,int>::iterator it = std::find_if(m.begin(), m.end(), nonzero);
std::cout << it->first << '\t' << it->second << std::endl;
return 0;
}
bool nonzero(std::map<char,int>::const_iterator& it) {
return it->second;
}
Run Code Online (Sandbox Code Playgroud)
g ++给出了非常复杂的错误,并说:
/usr/include/c++/5/bits/predefined_ops.h:234:30: error: invalid initialization of reference of type ‘std::_Rb_tree_const_iterator<std::pair<const char, int> >&’ from expression of type ‘std::pair<const char, int>’
{ return bool(_M_pred(*__it)); }
Run Code Online (Sandbox Code Playgroud)
我不明白它说的是什么以及为什么我的程序会失败.
我想知道是否可以使用std::back_inserter从更复杂的结构中创建仅包含单个元素的向量。例如,在以下代码中:
struct Person {
std::string first_name;
std::string last_name;
int id;
};
std::vector<Person> people;
// Populate 'people'...
// Extract last names into single vector
std::vector<std::string> last_names;
std::copy(begin(people), end(people), std::back_inserter(last_names)); // <... ?
Run Code Online (Sandbox Code Playgroud)
到目前为止,我发现的唯一解决方案是创建一个强制转换运算符Person -> std::string:
struct Person {
// ...
operator std::string() const { return last_name; }
};
Run Code Online (Sandbox Code Playgroud)
但是,如果我想将first_name和提取last_name到两个向量中,这不是一个好的解决方案,更不用说晦涩难懂的隐式转换了。
有什么方法可以指示std::back_inserter如何构造要插入的元素?任何其他方式来创建这样的向量?显然,我不是指原始方式:
std::vector<std::string> last_names;
last_names.reserve(people.size());
for (const auto& person : people) {
last_names.push_back(person.last_name);
}
Run Code Online (Sandbox Code Playgroud)
但对于一些<algorithm>类似的人 ;)
我更喜欢仅使用 C++ 的答案,但如果需要,我愿意接受 Boost 解决方案。
我有这个要求来找到向量中小于值的最后一个元素。
像find_first_of一样,但我要的不是最后一个。我搜索发现没有find_last_of,但是有find_first_of。
为什么?标准方法是将find_first_of与反向迭代器一起使用吗?
这不构建,我不理解编译错误.
#include <unordered_map>
#include <algorithm>
int main()
{
std::unordered_map<int, size_t> occurences = { { 10, 2 }, { 20, 5 }, { 30, 0 }, { 40, 5 }, { 50, 0 }, { 100, 9 } };
auto newEnd = std::partition(occurences.begin(), occurences.end(), [](const std::pair<int, size_t> &p)
{
return p.second == 0;
});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++抱怨如下.VS2013更加神秘.
/usr/local/include/c++/6.3.0/bits/stl_pair.h:实例化'void std :: pair <_T1,_T2> :: swap(std :: pair <_T1,_T2>&)[与_T1 = const int; _T2 = long unsigned int]':/ usr/local/include/c + +/6.3.0 /bits/stl_pair.h:473:7:从'void …