出于各种原因(我向你保证它们是有效的,所以请不要谈论“使用 Cocoa”),我必须使用 XCode、C++、OpenGL、OpenCL(旁边有一点 GLUT)在 Mac 上重建一些图形演示(来自 XP + Visual Studio 2005 开发)。该项目是使用“c++ stdc++”作为命令行工具构建的。
我的 Program.h 文件将我的着色器对象连接在一起、编译、链接并以其他方式准备它们以用作 OpenGL 着色器程序。此文件中包含以下相关代码行:
#include <vector>
using std::vector;
Run Code Online (Sandbox Code Playgroud)
并在班级的私人部分中:
vector<int> shaderHandles;
Run Code Online (Sandbox Code Playgroud)
添加着色器句柄时:
shaderHandles.push_back(shaderHandle);
Run Code Online (Sandbox Code Playgroud)
最后,当使用推送的着色器句柄时:
for (int s = 0; s < (int) shaderHandles.size(); s++)
{
glAttachShader(handle, shaderHandles[s]);
}
Run Code Online (Sandbox Code Playgroud)
根据我的所有经验和研究,C++ 中的这些行没有任何问题。但是在编译的时候(无论是debug还是release,所以与_GLIBCXX_DEBUG问题无关),会产生以下4个错误:
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_bvector.h:916: error: 'size' is not a member of 'std'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_bvector.h:961: error: 'size' is not a member of 'std'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/vector.tcc:350: error: '__old_size' is not a member of 'std'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/vector.tcc:453: error: '__old_size' is not a member …Run Code Online (Sandbox Code Playgroud) 我正在试验 C++11(到目前为止我已经使用了旧的 C++)并且我编写了以下代码:
#include <iostream>
#include <vector>
#include <type_traits>
using namespace std;
constexpr bool all_true(){
return true;
}
template <typename Head, typename... Tail>
constexpr bool all_true(Head head, Tail... tail){
static_assert( is_convertible<bool, Head>::value, "all_true arguments must be convertible to bool!");
return static_cast<bool>(head) && all_true(tail...);
}
template<typename T, typename... Args>
void print_as(Args... args){
static_assert( all_true(is_convertible<T,Args>::value...), "all arguments must be convertible to the specified type!");
vector<T> v {static_cast<T>(args)...};
for(T i : v) cout << i << endl;
}
int main(){
print_as<bool>(1, 2, 0, …Run Code Online (Sandbox Code Playgroud) 我有一个代码,其中在函数的末尾我需要从 int 转换为数组的所有元素的两倍,以便能够在退出函数之前执行最终 push_back。我现在拥有的代码是:
template <class T, size_t dims> class A {
typedef typename std::array<int, dims> ArrayInt;
typedef typename std::array<double, dims> ArrayDouble;
typedef typename std::vector <ArrayDouble> VectorDouble;
/* ...*/
foo() {
/* ...*/
ArrayInt myArrayInt;
ArrayDouble myArrayDouble;
VectorDouble myVectorDouble;
/* Initialize myArrayInt
Do some other stuff */
for (int i = 0; i < dims; ++i)
myArrayDouble[i] = static_cast<double>(myArrayInt[i]);
myVectorDouble.push_back(myArrayDouble);
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我对这些行感到不舒服:
for (int i = 0; i < dims; ++i)
myArrayDouble[i] = static_cast<double>(myArrayInt[i]);
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
谢谢你。
我有一个类包装了大量的网络数据包字节。该类实现一个队列并提供(除其他外)front()函数,该函数返回构成队列中最旧数据包的字节常量向量。
class Buffer{
unsigned char data[65536];
unsigned int offset;
unsigned int length;
[...]//other fields for maintaining write ptr etc.
public:
const std::vector<unsigned char> front(){
return std::vector<unsigned char>(data + offset, data + offset + length);
}
//other methods for accessing the queue like
//pop(), push(), clean() and so forth...
[...]
}
Run Code Online (Sandbox Code Playgroud)
上述front()函数实现的性能受到从当前数据包占据的范围内不必要的复制字节的影响。由于向量是常量,因此无需复制数据。我想要的是在已经存储在缓冲区中的数据上创建一个向量。当然,向量的析构函数不应该释放内存。
我的目标是尝试解决这个问题:从 end 中删除向量中的所有空元素。使用擦除删除成语。
这个想法是在给定std::vector<std::string>的字符串a中删除从末尾开始的所有元素,这些元素是空的(等于空白)。当发现非空元素时,应停止删除元素。
例子:
vec = { " ", "B", " ", "D", "E", " ", " ", " " };
Run Code Online (Sandbox Code Playgroud)
移除后:
vec = { " ", "B", " ", "D", "E"};
Run Code Online (Sandbox Code Playgroud)
这是我尝试的解决方案:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
int main()
{
std::vector<std::string> vec = { " ", "B", " ", "D", "E", " ", " ", " " };
bool notStop = true;
auto removeSpaceFromLast = [&](const std::string& …Run Code Online (Sandbox Code Playgroud) 对于以下代码,我遇到了一个奇怪的运行时错误:
#include <algorithm>
#include <vector>
using std::vector;
struct Data
{
int id;
};
int main()
{
vector<Data> mylist;
Data m;
m.id = 10;
mylist.push_back(m);
mylist.erase(std::remove_if(
mylist.begin(),
mylist.end(),
[](const Data &m) {
return m.id>100;
}));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误说:
向量擦除迭代器超出范围
我有一些 C++ 代码通过pybind11绑定到 Python ,我们遇到了一个奇怪的问题,即通过虚拟方法通过引用传递 std::vector 并且在该方法之外保留对该 std::vector 的更改。
我们想要做的是提供一个通过引用接受 std::vector 的 C++ 纯虚函数,以便覆盖此方法的 Python 类可以就地修改该 std::vector,将其交还给其他 C++ 代码。我们正在关注关于使STL 代码不透明的pybind11 文档,但在我们的测试中,我们发现在 Python 中创建 STL 向量时,STL 向量被正确修改并通过引用传递,但是如果我们在C++ 不是。
这是一个独立的示例,演示了该问题。首先是 C++ pybind11 代码,我们在其中创建一个抽象基类,其中A包含一个A::func我们希望从 Python 覆盖的纯虚方法:
#include "pybind11/pybind11.h"
#include "pybind11/stl_bind.h"
#include "pybind11/stl.h"
#include "pybind11/functional.h"
#include "pybind11/operators.h"
#include <iostream>
#include <vector>
namespace py = pybind11;
using namespace pybind11::literals;
PYBIND11_MAKE_OPAQUE(std::vector<int>)
//------------------------------------------------------------------------------
// The C++ types and functions we're binding
//------------------------------------------------------------------------------
struct A {
virtual void …Run Code Online (Sandbox Code Playgroud) 我遇到了该push_back函数的以下语法。Vertex只是一个包含三个浮点数 x、y 和 z 的结构。第二行看起来就像会写它。但是第一行对我来说看起来很奇怪。在解释这一点的视频中,据说这是通过成员初始值设定项列表完成的,但它看起来更像是隐式转换。我只是对那里的大括号感到困惑。谁能解释为什么这种语法有效?
std::vector<Vertex> vertices;
vertices.push_back({ 1, 2, 3 });
vertices.push_back(Vertex(1, 2, 3));
Run Code Online (Sandbox Code Playgroud) 我想这样做
#include <vector>
#include <span>
struct S
{
std::vector<int> v;
void set(std::span<int> _v)
{
v = _v;
}
};
Run Code Online (Sandbox Code Playgroud)
但它不编译。有哪些替代方案?
我有一个很大的 .txt 文件,需要加载并存储在向量中。该文件大小约为 5MB,500 000 行,每行约 10-20 个字符,以 '\n' 分隔。我正在使用以下示例代码对读取整个文件所需的时间进行一些基准测试:
#include<iostream>
#include<vector>
#include<fstream>
int main()
{
std::fstream input("words.txt");
std::vector<std::string> vector;
std::string line;
while(input >> line){
vector.push_back(line);
}
}
Run Code Online (Sandbox Code Playgroud)
我很好奇将字符串作为右值引用传递是否会更快,但它会慢大约 10 毫秒。
#include<iostream>
#include<vector>
#include<fstream>
int main()
{
std::fstream input("words.txt");
std::vector<std::string> vector;
std::string line;
while(input >> line){
vector.push_back(std::move(line));
}
}
Run Code Online (Sandbox Code Playgroud)
第一个代码示例的平均加载时间约为 58 毫秒,第二个代码示例的平均加载时间为 68-70 毫秒。我在想移动总是更快或等于复制,这就是为什么这对我来说似乎不正确。
有谁知道发生这种情况的原因?基准测试是使用以下方法完成的:
perf stats -r 100 ./a.out
Run Code Online (Sandbox Code Playgroud)
在 Arch Linux 上,代码已使用 GCC 10.2、C++17 std 编译。
此外,如果有人知道这样做的更佳方法,我们将不胜感激。