输入是存储在连续存储器中的比特阵列,每1比特存储器具有1比特的比特阵列.
输出是比特阵列的设定位索引的数组.
例:
bitarray: 0000 1111 0101 1010
setA: {4,5,6,7,9,11,12,14}
setB: {2,4,5,7,9,10,11,12}
Run Code Online (Sandbox Code Playgroud)
获得A组或B组都可以.该集存储为uint32_t数组,因此该集的每个元素都是数组中的无符号32位整数.
如何在单个cpu核心上快5倍左右?
当前代码:
#include <iostream>
#include <vector>
#include <time.h>
using namespace std;
template <typename T>
uint32_t bitarray2set(T& v, uint32_t * ptr_set){
uint32_t i;
uint32_t base = 0;
uint32_t * ptr_set_new = ptr_set;
uint32_t size = v.capacity();
for(i = 0; i < size; i++){
find_set_bit(v[i], ptr_set_new, base);
base += 8*sizeof(uint32_t);
}
return (ptr_set_new - ptr_set);
}
inline void find_set_bit(uint32_t n, uint32_t*& ptr_set, uint32_t base){
// Find the set bits …Run Code Online (Sandbox Code Playgroud) 获取第一个元素的地址std::vector并像原始数组一样使用它是否安全?数据是否连续排列?什么c ++标准说(c++98vs c++11)?这是一种常见的做法吗?
我有一些大小可能变化的数组和函数,它将指针和元素数量作为参数.管理数据并std::vector同时使用它会很棒my_func.
std::vector<int> my_vector;
void my_func(int* int_ptr, int num_ints);
// ..
my_func(&my_vector[0], my_vector.size());
Run Code Online (Sandbox Code Playgroud) 交换两个元素后,向量是否会保持连续状态?
PS:无论答案是什么,我们怎么能确定?如果那是可能的.
我是C++的新手(来自Java),我想在类中声明一个数组对象,但它的模板参数需要一个整数值.我想我必须创建一个指向数组类的指针,但它不起作用..
我想做一些像:
class foo{
private:
array *myArray;
public:
foo(int size){
//This line may be terribly wrong, but you see what I mean
myArray = new array<int,5>();
}
~foo(){
free(myArray);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,数组对象的正确初始化是:
array<int,5>
Run Code Online (Sandbox Code Playgroud)
但这种方式不允许我在运行时选择长度.
我有一个运行的线程从串行端口读取字节流.它在后台持续执行此操作,并且从流中读取的内容分别在不同的时间进行.我将数据存储在一个容器中,如下所示:
using ByteVector = std::vector<std::uint8_t>;
ByteVector receive_queue;
Run Code Online (Sandbox Code Playgroud)
当数据从串行端口进入时,我将它附加到字节队列的末尾:
ByteVector read_bytes = serial_port->ReadBytes(100); // read 100 bytes; returns as a "ByteVector"
receive_queue.insert(receive_queue.end(), read_bytes.begin(), read_bytes.end());
Run Code Online (Sandbox Code Playgroud)
当我准备好读取接收队列中的数据时,我将其从前面删除:
unsigned read_bytes = 100;
// Read 100 bytes from the front of the vector by using indices or iterators, then:
receive_queue.erase(receive_queue.begin(), receive_queue.begin() + read_bytes);
Run Code Online (Sandbox Code Playgroud)
这不是完整的代码,但是很好地了解了我如何利用向量来实现这种数据流机制.
我对这个实现的主要关注是从前面移除,这需要移除每个元素(我不确定如何erase()对向量进行优化,但在最坏的情况下,每个元素移除导致整个向量的移位).另一方面,由于数据的连续性,向量是CPU高速缓存局部性的候选者(但不保证CPU高速缓存的使用).
我想过可能会使用boost::circular_buffer,但我不确定它是否适合这项工作.
我还没有为接收队列的增长编写一个上限,但是我可以很容易地做reserve(MAX_RECEIVE_BYTES)某个地方,并确保它size()永远不会超过MAX_RECEIVE_BYTES我继续附加到它的后面.
这种方法一般都可以吗?如果没有,那有什么性能问题?什么容器在这里更合适?
我有一些 15 年前的 C++ 代码,我正试图将它们带到更现代的时代。在这个阶段,我试图让用 Visual C++ 6.0 编译的代码现在用 VS 2003 (Microsoft Visual C++ .NET 69462-335-0000007-18915) 编译。
std::vector<myClassType> myVect;
...
// Assuming vector.begin() simply points to start of a contiguous array
bool OK = File.write((char*) myVect.begin(),
myVect.size() * sizeof(myClassType));
Run Code Online (Sandbox Code Playgroud)
当我阅读评论时,我很担心......我们真的可以假设向量在连续内存中吗?如果没有,有什么更好的方法可以做到这一点?我是否需要遍历向量内容并从 myClassType 写入每组数据?
事实上,编译器抱怨无论如何将 begin() 的结果转换为 char* 。