我正在使用一个外部库,它在某些时候给了我一个指向整数数组和大小的原始指针。
现在我想使用std::vector
来访问和修改这些值,而不是使用原始指针访问它们。
这是一个解释这一点的人为示例:
size_t size = 0;
int * data = get_data_from_library(size); // raw data from library {5,3,2,1,4}, size gets filled in
std::vector<int> v = ????; // pseudo vector to be used to access the raw data
std::sort(v.begin(), v.end()); // sort raw data in place
for (int i = 0; i < 5; i++)
{
std::cout << data[i] << "\n"; // display sorted raw data
}
Run Code Online (Sandbox Code Playgroud)
预期输出:
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
原因是我需要<algorithm>
在该数据上应用算法(排序、交换元素等)。
在另一方面改变这种载体的大小将永远不会改变,因此push_back
, …
假设我有一个整数向量:
std::vector<int> indices;
for (int i=0; i<15; i++) indices.push_back(i);
Run Code Online (Sandbox Code Playgroud)
然后我按降序排序:
sort(indices.begin(), indices.end(), [](int first, int second) -> bool{return indices[first] > indices[second];})
for (int i=0; i<15; i++) printf("%i\n", indices[i]);
Run Code Online (Sandbox Code Playgroud)
这会产生以下结果:
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
Run Code Online (Sandbox Code Playgroud)
现在我想将数字 3、4、5 和 6 移到最后,并保持它们的降序(最好不必sort
第二次使用)。即,这是我想要的:
14
13
12
11
10
9
8
7
2
1
0
6
5
4
3
Run Code Online (Sandbox Code Playgroud)
我应该如何修改 的比较功能std::sort
来实现这一点?
我正在尝试创建一个有助于处理 Nstd::variant
类型的函数。
注意:我正在尝试验证所有路径的编译时间。所以std::optional
和std::holds_alternative
不可行我。
实现如下:
template<typename T>
using Possible = std::variant<std::monostate, T>;
template<typename... Types>
void ifAll(std::function<void(Types...)> all, Possible<Types>&&... possibles)
{
std::visit(
[&](auto&&... args) {
if constexpr ((... &&
std::is_same_v<std::decay_t<decltype(args)>, Types>))
{
return all(std::forward<Types>(args)...);
}
else
{
std::cout << "At least one type is monostate" << std::endl;
}
},
possibles...);
}
Run Code Online (Sandbox Code Playgroud)
使用该函数的一个例子是:
int main()
{
Possible<int> a = 16;
Possible<bool> b = true;
ifAll([](const int& x, const bool& y)
-> void { std::cout << "All …
Run Code Online (Sandbox Code Playgroud) 我有一个数组char* source
和一个向量std::vector<char> target
。我想让向量target
指向source
O(1),而不复制数据。
沿着这些路线的东西:
#include <vector>
char* source = new char[3] { 1, 2, 3 };
std::vector<char> target;
target.resize(3);
target.setData(source); // <- Doesn't exist
// OR
std::swap(target.data(), source); // <- swap() does not support char*
delete[] source;
Run Code Online (Sandbox Code Playgroud)
为什么不能手动更改矢量指向的位置?如果可能的话,是否会出现一些特定的、无法管理的问题?
看起来std::hardware_destructive_interference_size
没有在 libc++ 或 libstdc++ 中定义。我已经搜索了我的本地安装。我查看了各自的 svn 存储库。
它们应该在<new>
标题中,请参阅:cppreference.com
我希望人们会问这些常量在哪里,但显然没有其他人似乎错过了它们。
我错过了什么?他们在哪里!
是否有一种优雅的 STL 方法可以true
在给定索引的数组中找到最接近的(1) 值。例如,对于给定索引 5std::vector<int> v{1,1,1,0,0,0,1,1,0};
的最接近true
值位于索引 6。
我尝试并最终使用了多个带有迭代器的 while 循环。是否可以使用 C++ STL?
给定以下向量:
std::vector<int> foo{1,2,3,4,5,6,7};
std::vector<int> bar{10,20,30,40,50,60,70};
Run Code Online (Sandbox Code Playgroud)
最后,我想foo
包含{ 10, 2, 30, 4, 50, 6, 70 }
表示替换所有奇数的值。
我已经尝试过 std::replace_if 算法,但是如何访问条形对应的值?
// replace_copy_if example
#include <iostream> // std::cout
#include <algorithm> // std::replace_copy_if
#include <vector> // std::vector
bool IsOdd (int i) { return ((i%2)==1); }
int main () {
std::vector<int> foo{1,2,3,4,5,6,7};
std::vector<int> bar{10,20,30,40,50,60,70};
std::replace_if (foo.begin(), foo.end(), IsOdd, 0);
std::cout << "foo contains:";
for (auto i: foo){ std::cout << ' ' << i; }
std::cout << '\n';
return 0;
}
// output …
Run Code Online (Sandbox Code Playgroud) 我想将指向Object
类的共享指针存储在向量中:
测试代码:
#include <vector>
#include <iostream>
#include <memory>
using namespace std; // only for brevity
class Object
{
public:
int n;
Object(int n) : n(n) { cout << "Object(" << n <<")\n"; }
~Object() { cout << "~Object(" << n << "))\n"; n = 0xdddddddd; }
};
void Test()
{
std::shared_ptr<Object> p1(make_shared<Object>(Object(123)));
std::vector<shared_ptr<Object>> v;
cout << "before push_back\n";
v.push_back(std::make_shared<Object>(Object(2)));
v.push_back(p1);
cout << "after push_back\n";
cout << "Vector content:\n";
for (auto& p : v)
cout << " " << …
Run Code Online (Sandbox Code Playgroud) 以下代码尝试使用概念对类进行部分特化,并向特化添加方法,但被 clang 11.0.0 拒绝:
#include <concepts>
template <typename T> // note: previous template declaration is here
struct S {};
template <std::integral T>
struct S<T>
{
void f();
};
template <std::integral T> // error: type constraint differs in template redeclaration
void S<T>::f()
{
}
Run Code Online (Sandbox Code Playgroud)
clang 给出了错误信息:
<source>:14:16: error: type constraint differs in template redeclaration
template <std::integral T>
^
<source>:3:11: note: previous template declaration is here
template <typename T>
Run Code Online (Sandbox Code Playgroud)
(参见https://godbolt.org/z/Wv1ojK)。为什么这段代码是错误的?或者这是clang中的一个错误?(FWIW,此代码被 gcc trunk 和 MSVC 19.28 接受,尽管这不能保证正确性。)
我无法推断我可以使用文档中的std::set_difference,因为它说集合应该排序,这意味着它们不是集合,而是列表。此外,所有示例都是关于有序列表,而不是集合。
如何知道真相?