我指的是一个比较qsort vs stdsort性能的上一个链接.
我写了一个C程序,填充了一个大的std::map,我想对数组进行排序qsort.我正在使用.
typedef std::map<uint16_t, uint32_t> TSrcMap;
TPSrcMap sp;
TSrcMap::iterator its;
/*Code to populate the array_start.*/
/*Code to populate the array_end.*/
typedef struct port_count
{
uint32_t port_number;
uint32_t port_count;
}port_count_t;
port_count_t pcount[10];
memset(pcount,0,sizeof(pcount));
size_t structs_len = sizeof(pcount)/sizeof(port_count_t);
for(its = stcp.begin(); its != stcp.end();its++)
{
if(pcount[smallest_index].port_count < (*its).second)
{
pcount[smallest_index].port_count = (*its).second;
pcount[smallest_index].port_number = (*its).first;
/*qsort(pcount, structs_len, sizeof(port_count_t), struct_cmp_by_port_count);*/
std::sort(pcount,sizeof(port_count_t));
}
}
Run Code Online (Sandbox Code Playgroud)
该qsort函数正确排序数组.我想比较qsortwith 的性能,std::sort但调用std::sort调用给出了编译错误
没有匹配的呼叫功能
‘sort(port_count_t [10], …
我在我的代码中使用了许多名称空间,包括std,所以当我想在我的代码中声明一个字符串变量时,我应该精确地使用std :: string或者我可以放置字符串:
#include <string.h>
using namespace std;
using namespace boost;
using namespace xerces;
int main()
{
/*! should I declare my str like this */
std::string str;
/*! or I can declare it like this */
string str1;
cout << str << str1 <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 编辑:在我的代码版本中,aList被引用为指针,但在我当前的版本中仍然存在同样的问题.
在尝试将其拆分为接口文件和实现文件之前,我已完全使用此代码.但是当我拆分它时,编译器告诉我我用不正确的参数调用push_back().所以我理解它不能引用我推动它的对象的类型,虽然它是相同的(afaik,当然:D).
#ifndef _MYHEADER_HPP_
#define _MYHEADER_HPP_
class A{
public:
std::string someString;
};
class B{
public:
std::vector<A> aList;
public:
void addA();
};
#endif /* _MYHEADER_HPP_ */
//implementation file
#include <string>
#include <vector>
#include "myheader.hpp"
void B::addA(){
A a;
a.someString = "Hola";
// Here compiler says : Invalid arguments 'Candidates are: void push_back(const A &)' line 18 Semantic Error
aList.push_back(a);
}
Run Code Online (Sandbox Code Playgroud)
AFAIK,std :: vector总是执行要推送的对象的副本,并且该副本存储到向量中,所以我认为这不是"a"被分配的问题,我是对的吗?
我做错了什么?谢谢.
我偶然发现了以下网站:http : //www2.research.att.com/~bs/C++0xFAQ.html#lambda,他们在其中解释了lambda函数。我尝试使用提供的示例:
vector<int> indices( notImportantNumber );
int count = 0;
fill(indices.begin(), indices.end(), [&](){ return ++count; });
Run Code Online (Sandbox Code Playgroud)
同样
generate(indices.begin(), indices.end(), [&](){ return ++count; });
Run Code Online (Sandbox Code Playgroud)
虽然,当我尝试将示例与fill一起使用时,我继续遇到此错误:
错误1错误C2440:'=':无法从'const`anonymous-namespace'::'转换为'long'c:\ program files \ microsoft visual studio 10.0 \ vc \ include \ xutility 2692
有人知道为什么会这样吗?在的声明中std::fill(),没有函子作为最后一个参数。
我没有找到完全相同的问题.
有没有办法转换:
std::vector<boost::shared_ptr<T>>
Run Code Online (Sandbox Code Playgroud)
进入非安全版本:
std::vector<T*>
Run Code Online (Sandbox Code Playgroud)
没有做一个for循环并在原始向量的每个元素上使用.get()?
我知道标题有点模糊,但我现在想不出更好的标题.我的代码中的摘录如下所示:
#include<iostream>
#include<fstream>
int main(){
ifstream f("cuvinte.txt");
f.getline(cuvant);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我想阅读"cuvinte.txt"中的下一个单词时,我写了f.getline(cuvant); 但我得到以下错误
error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
Run Code Online (Sandbox Code Playgroud)
我不知道问题是什么,我不久前偶然发现了这个问题,仍然无法超越它.
我编写了一个包装器类,以对类型向量执行插入/删除操作。代码:
class GenericSymbolTable {
public:
virtual void pushBackAtom(Atom *atom) = 0;
virtual Atom* peekAtom(void) = 0;
virtual Atom* getAtom(void) = 0;
protected:
~GenericSymbolTable(void){}
};
class SymbolTable : public GenericSymbolTable {
private:
vector<Atom*> atoms;
protected:
~SymbolTable(void);
public:
void pushBackAtom(Atom *atom);
Atom* peekAtom(void);
Atom* getAtom(void);
};
Run Code Online (Sandbox Code Playgroud)
在为这些方法编写实现时,编译器会引发冲突的类型错误:
Atom* SymbolTable::peekAtom(void) {
if(atoms.empty()) {
cout << "\t[W] Simbol table does not contain any atoms" << endl;
return NULL;
}
Atom* first = atoms.begin(); // <== type error
return first;
}
Atom* SymbolTable::getAtom(void) {
if(atoms.empty()) …Run Code Online (Sandbox Code Playgroud) 如何应用std::random_shuffle算法std::queue?我试过这个:
std::random_shuffle(myQueue.front(), myQueue.back());
Run Code Online (Sandbox Code Playgroud)
并给出错误:
我的队列正在举行Card代表扑克牌的课程.我可以理解,错误来自于对std::random_shuffle队列元素进行的操作所以,即使我不需要!= operator为我的Card类,我写了一个,并且该错误消失了.
但是我应该怎么处理其余的错误呢?operators +, - and ++为一Card堂课写作是没有意义的.
我打印出std :: string的字节表示时遇到奇怪的错误,而std :: wstring工作正常.
std::string str = "mystring";
unsigned short* vtemp = (unsigned short*)str.c_str();
for(int i=0; i<str.length(); ++i)
{
cout << (unsigned short)((unsigned char)vtemp[i]) << " ";
}
cout << endl;
Incorrect Output: 109 115 114 110 0 204 204 204
wstring wstr(str.length(), L' ');
std::copy(str.begin(), str.end(), wstr.begin());
vtemp = (unsigned short*)wstr.c_str();
for(int i=0; i<wstr.length(); ++i)
{
cout << (unsigned short)((unsigned char)vtemp[i]) << " ";
}
cout << endl;
Correct Output: 109 121 115 116 114 105 110 103 …Run Code Online (Sandbox Code Playgroud) 我使用C++ 11创建了一个如下的threadpool类.
#include <iostream>
#include <functional>
#include <vector>
#include <condition_variable>
#include <thread>
#include <mutex>
#include <queue>
#include <atomic>
#include <future>
class threadpool {
struct task {
std::size_t m_priority;
std::function<void()> m_fn;
bool operator()(task* t1, task* t2) const {
return t1->m_priority < t2->m_priority;
}
};
public:
threadpool(std::size_t nthreads);
~threadpool();
// void assign(std::function<void()> fn, std::size_t priority);
template <typename T> std::future<T> assign(std::function<T()> fn, std::size_t priority);
private:
void funnel();
std::vector<std::thread*> m_threadlist;
std::priority_queue<task*, std::vector<task*>, task> m_tasks;
std::mutex m_tasks_mtx;
std::condition_variable m_newc;
std::mutex m_newc_mtx;
std::atomic<bool> m_destruct;
};
threadpool::threadpool(std::size_t …Run Code Online (Sandbox Code Playgroud) c++ ×10
std ×10
namespaces ×2
boost ×1
collections ×1
dictionary ×1
iterator ×1
lambda ×1
linker ×1
qsort ×1
queue ×1
shuffle ×1
stdmap ×1
stdvector ×1
string ×1
templates ×1
using ×1
vector ×1
visual-c++ ×1