我们如何在c ++中的类构造函数中初始化std :: vector?
class MyClass
{
public:
MyClass( int p_Var1, int* p_Vector ) : Var1( p_Var1 ) //, Initialize std::vector - MyVector with p_Vector
{
};
~MyClass( void );
private:
int Var1;
std::vector< int > MyVector;
};
Run Code Online (Sandbox Code Playgroud) 我想调用一个库函数,其签名是:
bool WriteBinary(const std::vector<uint8_t> & DataToWrite);
Run Code Online (Sandbox Code Playgroud)
我有一个std::string变量,我想把它作为参数发送给它.
void WriteString(const std::string & TextData)
{
// ...
WriteBinary(TextData); // <-- How to make this line work?
// ...
}
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以直接发送std::string变量而无需复制它?
我的目的是将逗号分隔的值列表解析为嵌套向量.这个清单是二维的.基本问题是:
像"牵引"下的表格:
'
' RPM
0,5000,10000,15000,20000,25000
'
' Temp
'
-40.,0.,20.,40.
'
' Traction
200.,175.,170.,165.,160.,150.
200.,175.,170.,165.,160.,150.
165.,165.,160.,155.,145.,145.
160.,155.,150.,145.,145.,140.
'
Run Code Online (Sandbox Code Playgroud)
在接下来的步骤中,我想阅读4维数据,但是现在我正在努力解决第二个问题.数据结构如下:
struct table {
std::vector<double> index;
std::vector<double> index2;
std::vector<std::vector<double> > base;
};
Run Code Online (Sandbox Code Playgroud)
语法是恕我直言,非常简单如下:
comment %= qi::lexeme[ '\'' >> *(qi::standard::char_ - qi::eol)] >> qi::eol;
commentblock = comment >> *(comment);
doublevector = qi::double_ % ',' >> qi::eol ;
vectorblock = *doublevector;
start = commentblock >>
doublevector >>
commentblock >>
doublevector >>
commentblock >>
vectorblock >>
commentblock >>
qi::eoi
;
Run Code Online (Sandbox Code Playgroud)
到目前为止,我解析这两个向量 …
我想使用 std::transform 做一些类似于 binary_op 的事情,但有一个附加常量,例如,获得两个向量的乘积: x1 = (10,20,30,40,50) 和 x2 = (2, 4,6,8,10),我们可以写:
#include <iostream>
#include <algorithm>
#include <vector>
double multiply(double x, double y){
return x*y;
}
int main () {
std::vector<int> x1;
std::vector<int> x2;
for (int i=1; i<6; i++)
x1.push_back (i*10); //10,20,30,40,50
for (int i=1; i<6; i++)
x2.push_back (i*2); //2,4,6,8,10
std::transform (x1.begin(), x1.end(), x2.begin(), x1.begin(),multiply);
for (std::vector<int>::iterator it=x1.begin(); it!=x1.end(); ++it)
std::cout << ' ' << *it;
//output: 20,80,180,320,500
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将 x1 和 x2 按元素相乘并返回 (20,80,180,320,500)。
但是,如果我想计算 x1 …
C++ 标准建议 std::vector 呈指数增长,以便在重新分配方面具有“摊销不变成本”。
虽然这种类型的增长适用于大多数场景,但可能有一种情况我发现我需要使用不同的算法来增长向量。
有没有办法自定义 std::vector 的增长方式以及它在重新分配之前检查的条件?
根据cppref,在 C++20 中std::vector::push_back声明如下:
constexpr void push_back(const T& value);
Run Code Online (Sandbox Code Playgroud)
push_back我无法想象应该出现的场景constexpr。
背后的原理是什么?
有多种方法可以使向量为空,包括
std::vector<int> example = makeMyReallyBigVector();
example.clear();
example.resize(0);
example.erase(example::being(), example::end());
// any other method which would be relevant
Run Code Online (Sandbox Code Playgroud)
C++ 标准中是否有关于哪一个最有效、最省时的保证?内容是没有析构函数的原始数据类型。我特别关心的是,我不希望矢量容量发生变化,我只想将其内部“已使用大小”设置为 0,而不触及现在已擦除的内容。
我想要的是int在常数时间内将向量大小设置为 0,而不释放任何内存。这可以用 C++ 标准实现吗?
如果 C++ 标准没有提供任何保证,我正在使用 GNU C++ 标准库,所以如果标准没有提供任何保证?出于可移植性的考虑,在这种情况下,当然,有关 Clang 和 Microsoft 标准库的信息也会令人感兴趣。
如果我有两个 C++ 向量:
vector<int> a(5) {1,2,3,4,5};
vector<int> b(5) {6,7,8,9,10};
Run Code Online (Sandbox Code Playgroud)
有没有一种单行方法可以使用交换方法来交换 a 和 b 的切片?就像是
swap(a[something ... something], b[something ... something]);
Run Code Online (Sandbox Code Playgroud)
给予,例如
a等于{1,9,10,4,5}并b等于{6,7,8,2,3}?
我正在尝试 C++20 概念,但遇到了一个无法解释的奇怪问题。
\n我正在尝试定义一个concept检查给定迭代器是否可以递增(很好)的方法,并且还检查类型是否因此而改变(不好)。
原谅我的无知,我错过了什么?错误的最后一行指出:
\n\' 注意:\xe2\x80\x98++ it\xe2\x80\x99 不满足返回类型要求\n{ ++it } -> std::same_as; \'
\n#include <functional>\n#include <vector>\n\ntemplate<typename Iter>\nconcept Iterable = requires(Iter it){\n { ++it } // Check iterator can be incremented (compiles but does not check return type)\n { ++it } -> std::same_as<Iter>; // Also check the return type (**fails**)\n};\n\ntemplate<typename Iter>\nrequires Iterable<Iter>\nvoid doesNothing(Iter current, Iter end){\n while(current != end){\n ++current;\n }\n};\n\nint main(){\n std::vector<int> v = {1, 2, 3};\n doesNothing(v.begin(), v.end());\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n预先非常感谢您。 …
我可以简洁地(用大括号)初始化以下 6 种情况中的 5 种:
\n| 可复制的 | 只移动的 | |
|---|---|---|
| 大批 | 是的 | 是的 |
| 标准::数组 | 是的 | 是的 |
| std::向量 | 是的 | 不 |
一种似乎不起作用的情况是尝试初始化仅移动对象的 std::vector ;无法编译,并显示消息,例如:“错误:调用 \'std::unique_ptr\' 的隐式删除复制构造函数”。
\n为什么是这样?是否有适用于这种情况的替代初始化语法?
\n下面的程序演示了。
\n/*\nclang++ -std=c++14 -W -Wall -Werror question.cc -o question\nclang++ -std=c++17 -W -Wall -Werror question.cc -o question\nclang++ -std=c++20 -W -Wall -Werror question.cc -o question\nclang++ -std=c++2b -W -Wall -Werror question.cc -o question\ng++ -std=c++14 -W -Wall -Werror question.cc -o question\ng++ -std=c++17 -W -Wall -Werror question.cc -o question\ng++ -std=c++20 -W -Wall -Werror question.cc -o …Run Code Online (Sandbox Code Playgroud) c++ ×10
stdvector ×10
c++20 ×2
boost ×1
boost-spirit ×1
c++-concepts ×1
c++11 ×1
casting ×1
constexpr ×1
constructor ×1
lambda ×1
stdstring ×1
transform ×1