是否可以将输出std::set_intersection或其他std::set_*算法函数直接添加到类型中std::set?
到目前为止,我只能想到一种直接添加到允许std::back_inserter.
#include <set>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
int main()
{
// sorted so ok for set operations
set<int> s = { 2, 3, 1 };
set<int> s2 = { 1 };
vector<int> v;
set_intersection(s.begin(), s.end(), s2.begin(), s2.end(),
back_inserter(v));
copy(v.begin(), v.end(), ostream_iterator<int>(cout, ","));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我是 C++ 的新手。我正在尝试实现一个堆栈。我arr在默认构造函数中声明了一个命名变量。
但是当我编译我的代码时,我收到一条错误消息
'arr' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
我的代码:
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Stack
{
private:
int top = -1;
int n = 100;
public:
Stack()
{
int arr[n]; // 100 element stack
}
void push(int element)//push element to the top of the stack
{
if (isFull() == false)
{
// push element
top += 1; //increment top
arr[top] = element;
}
else cout << "\nStack is …Run Code Online (Sandbox Code Playgroud) 我想编写一个函数,该函数采用任何类型的通用容器并打印它。
让我们暂时搁置它不适用于某些关联容器并关注这个问题:
template<template<typename> typename Cont, typename T>
void print(const Cont<T>& cont)
{
for (const auto it : cont)
{
cout << it << " ";
}
cout << endl;
}
int main()
{
vector<string> v;
print(v);
}
Run Code Online (Sandbox Code Playgroud)
错误指出:
template<template<typename> typename Cont, typename T>
void print(const Cont<T>& cont)
{
for (const auto it : cont)
{
cout << it << " ";
}
cout << endl;
}
int main()
{
vector<string> v;
print(v);
}
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么编译器不能在这里推断出类型?
即使我明确声明print<vector<string>>(v)?
我需要获得一个基于偏移量的迭代器。
即有开始的迭代器:
auto it = set.begin()
Run Code Online (Sandbox Code Playgroud)
我需要到达具有偏移量的迭代器ofst:
it + ofst
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?我需要增量增加it++迭代器ofst次数。
我想为STL容器设计一个打印功能,包括:std::vector, std::map, std::unodered_map, std::set, std::unordered_set, std::list....
理想的函数如下所示:
template<typename T>
void Show(const T& t)
{
if (istype(t, std::vector))
{
// here is a presudo code
// print vector
for (auto i: t) cout << i << endl;
}
else if (istype(t, std::unordered_map))
{
// print unordered_map
}
else
{ }
}
Run Code Online (Sandbox Code Playgroud)
我认为问题发生在 istype()
我知道有一些功能std::is_same可以做到这一点。
但是好像只能操作int或者float不能操作std::vector,你能帮帮忙吗?
以下原型旨在进行同步打印:
#include <iostream>
#include <string>
#include <sstream>
#include <mutex>
#include <Windows.h> // for OutputDebugString
std::mutex sync_mutex;
template<typename T>
void sync_print_impl(std::ostringstream& str, const T& t)
{
str << t << " ";
}
template<typename ... Args>
void sync_print_impl(std::ostringstream& str, Args ... args)
{
str; // prevents unused variable warning when sync_print is called without arguments
(..., sync_print_impl(str, args));
}
template <typename... Args>
void sync_print(Args... args)
{
std::ostringstream s;
sync_print_impl(s, args...);
{
std::lock_guard<std::mutex> lg(sync_mutex);
std::cout << s.str() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
简单测试即可: …
vector<int> a{ 1,3,2 }; // initialize vectors directly from elements
for (auto example : a)
{
cout << example << " "; // print 1 5 46 89
}
MinHeap<int> p{ 1,5,6,8 }; // i want to do the same with my custom class
Run Code Online (Sandbox Code Playgroud)
知道如何在大括号中接受多个参数并形成一个数组吗?
std::vectorclass 用于std::allocator分配内存,但我不知道如何在自定义类中使用它。
VS Code 显示std::allocator
template<typename T>
class MinHeap
{
// ...
public:
MinHeap(size_t size, const allocator<T>& a)
{
cout << a.max_size << endl;
}
// ...
};
Run Code Online (Sandbox Code Playgroud)
菜鸟在这里....
有一个std::convertible_to<T>概念是检查调用结果是否可以转换为某种类型。
但我想检查函数是否具有确切的返回类型。我怎样才能做到这一点?
我想创建一个静态类,它将充当固定大小的内存分配器。
让我们看一下这个简化的例子:
struct A {};
struct B {};
template<class T, std::size_t MaxNumObjects>
class InternalContainer
{
public:
static constexpr std::size_t maxObjects = MaxNumObjects;
static T* createObject()
{
return ::new(&objects[idx++]) T;
}
static T* releaseObject() { /*...*/ };
private:
using buffer_t = std::aligned_storage_t<sizeof(T), alignof(T)>;
static buffer_t objects[maxObjects];
static std::size_t idx;
};
A* a = InternalContainer<A, 2>::createObject();
B* b = InternalContainer<B, 5>::createObject();
Run Code Online (Sandbox Code Playgroud)
如何根据类型预定义容器的最大尺寸?
// will be replaced "InternalContainer<A, 2>"
A* a = MyContainer<A>::createObject();
// will be replaced "InternalContainer<B, 5>"
B* b = MyContainer<B>::createObject(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试获取类的类型名称。当然,我可以只使用typename(class),但我需要将它放在这样的模板中
ClassA<typeid(class).name()> a = ClassA<typeid(class).name()>(args);
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
template<class T>
class A
{
public:
T t; // the class
A(T t1)
{
t = t1;
}
};
class B // example class
{
public:
int i;
B(int i1)
{
i = i1;
}
};
int main()
{
B b = B(1);
A<typeid(b).name()>(b) a = A<typeid(b).name()>(b); // my issue
}
Run Code Online (Sandbox Code Playgroud)
但它总是给我一个错误。
'A': invalid template argument for 'T', type expected.
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题(类A需要能够接受一个属性中的任何类)?
c++ templates class-template c++11 template-argument-deduction
c++ ×10
c++11 ×6
templates ×5
c++17 ×3
algorithm ×2
iterator ×2
c++-concepts ×1
c++20 ×1
class ×1
constructor ×1
function ×1
return-type ×1
stdset ×1
stdvector ×1