std 相关问题

C ++标准库及其命名空间。与[c ++]结合使用。



std::string 能够存储多个 ' '。怎么修剪?

如标题所示,下面的示例表明 std::string 能够存储多个字符串终止字符。如何修剪字符串,使 size() 和 length() 反映其实际长度...

回答 2 投票 0

std::可选成员是否连续存储?

我想我对如何存储可选值有点困惑。当构造包含 std::Optional 成员的类或结构时,这些成员是否会连续存储...

回答 2 投票 0

container.begin()+空容器vs容器的有效性1

<<" operator overload for printing vectors with cout was causing segfaults if used with empty

回答 1 投票 0

如何比较size_t和difference_type?

以下代码 #包括 #包括 int main() { std::vector v = { 1,2,3 }; if (std::count(v.begin(), v.end(), 1) > v.size() / 2) { 标准:...

回答 1 投票 0

如何使用字段名称初始化结构?

我想用不同的字段值来初始化某种类型(结构)的许多不同对象,并且我想使用名称进行初始化以使代码可读且健壮。 初始化器...

回答 1 投票 0

Visual Studio CMake 项目找不到 C++ 标准包含

这是一个不起作用的CMakeLists.txt。 我将一个项目从直接使用 WinAPI 接口迁移到使用 SDL2。这是生成的 CMakeLists.txt。除此之外,出于此目的

回答 1 投票 0

如何打印类型向量<tuple<string, int, int>>到屏幕C++?

假设我有 std::vector> tupleVector; tupleVector.push_back(std::tuple("乔", 2, 3)); tupleVector.push_back(std::tuple 假设我有 std::vector<std::tuple<string ,int ,int>> tupleVector; tupleVector.push_back(std::tuple<string ,int ,int>("Joe", 2, 3)); tupleVector.push_back(std::tuple<string ,int ,int>("Bob", 4, 5)); 如何迭代向量以打印包含元组的该向量的所有值? 只需迭代 vector,使用 tuple: 打印每个 cout 值 for (const auto& i : tupleVector) { cout << get<0>(i) << ", " << get<1>(i) << ", " << get<2>(i) << endl; } 您需要将问题分成两步。首先考虑如何仅打印元组,然后考虑如何打印向量。这是我的做法: std::ostream& operator<<(std::ostream& s, const std::tuple<std::string, int, int>& t) { s << "(" << std::get<0>(t) << "," << std::get<1>(t) << "," << std::get<2>(t) << ")"; return s; } std::ostream& operator<<(std::ostream& s, const std::vector<std::tuple< std::string, int, int> >& v) { s << "["; for (size_t idx = 0; idx < v.size(); idx++) { s << v[idx]; if (idx < v.size() - 1) { s << ","; } } s << "]"; return s; } int main() { std::vector<std::tuple<std::string, int, int> > v; v.emplace_back("hello", 3, 4); v.emplace_back("goodbye", 45, 67); std::cout << v << std::endl; return 0; } 此方法覆盖运算符<< for the tuple and the vector. Printing the vector will loop through the vector calling the operator<< for each tuple. 输出将是: [(hello,3,4),(goodbye,45,67)] 为了漂亮地打印任何项目(包括元组)的任何数组,就像这样。 注意:这个程序是用c++11编写的。 c++14 将使迭代元组变得更容易,而无需递归。 完全可编译的示例: #include <iostream> #include <tuple> #include <string> #include <vector> namespace detail { template<typename Stream> struct printer { printer(Stream& os) : _os ( os ) {} ~printer() { _os << " }"; } template<class X> void operator()(const X& x) { if (_first) { _os << "{ "; _first = false; } else { _os << ", "; } _os << x; } private: Stream& _os; bool _first = true; }; template<size_t index, size_t limit> struct print_loop { template<class Stream, class...Args> void operator()(detail::printer<Stream>&& print, const std::tuple<Args...>& tuple) const { print(std::get<index>(tuple)); print_loop<index+1, limit>()(std::forward<detail::printer<Stream>>(print), tuple); } }; template<size_t limit> struct print_loop<limit, limit> { template<class Stream, class...Args> void operator()(detail::printer<Stream>&& print, const std::tuple<Args...>& tuple) const { } }; } template<class Stream> detail::printer<Stream> make_printer(Stream& os) { return detail::printer<Stream>(os); } template<class Stream, class...Args> void print_elements(detail::printer<Stream>&& printer, const std::tuple<Args...>& tuple) { detail::print_loop<0, sizeof...(Args)>()(std::forward<detail::printer<Stream>>(printer), tuple); } template<class...Args> void tuple_print(std::ostream& os, const std::tuple<Args...>& tuple) { print_elements(make_printer(os), tuple); } template<class...Args> inline std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& tuple) { tuple_print(os, tuple); return os; } template<class T> inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) { auto print = make_printer(os); for(const auto& item : vec) { print(item); } } using namespace std; int main() { cout << "Hello World" << endl; auto x = make_tuple(string { "hello" }, 1, 2); cout << x << endl; auto y = vector<tuple<string, int, int>> { make_tuple(string { "hello" }, 3, 4), make_tuple(string { "world" }, 5, 6) }; cout << y << endl; return 0; } 示例输出: Hello World { hello, 1, 2 } { { hello, 3, 4 }, { world, 5, 6 } }

回答 3 投票 0

为什么一个视图不能分配同类型的视图?

为什么我不能(重新)为具有相同类型视图的视图分配值? 演示:https://godbolt.org/z/aqdh7ohva #包括 #包括 #包括 int主要...

回答 1 投票 0

为什么同类型的view也无法分配?

为什么即使具有相同类型的视图,我也无法(重新)为视图分配值? 演示(https://godbolt.org/z/aqdh7ohva) #包括 #包括 #包括 INT...

回答 1 投票 0

如何用过滤后的范围更新范围?

我有一个过滤器,应该有条件地应用于某个范围,并且条件仅在执行时已知。 我可以简单地做到这一点,将条件放入过滤器中,但这会导致高

回答 1 投票 0

为什么Qt的SimpleTreeModel示例使用std::vector<std::unique_ptr<>>而不是简单的std::vector<>?

我在一个项目中使用Qt 6.5,并尝试重构/改进最初在Qt 5.10中编写的树模型。我花了一些时间查看 Qt 的 SimpleTreeModel 示例并成功使用了 co...

回答 1 投票 0

为什么我无法压缩左值生成器?

以下简单程序编译并运行: std::generator get_ints(int n) { 对于 (int i{0}; i < n; ++i) co_yield i; } int main(int argc, char* argv[]) { std::vec...

回答 1 投票 0

如何针对特定概念(需求)重载使用 lambda 语法定义的函数?

我需要针对特定概念(要求)重载 lambda。它在模板语法中工作得很好,但当然“lambda 不是函数”并且不能重载。 不幸的是...

回答 1 投票 0

标准库中构造函数参数类型异常的问题

在标准库中,异常类的构造函数的参数类型,例如std::runtime_error,遵循以下模式: 运行时错误(const std::string&what_arg); // (1) 运行时_e...

回答 1 投票 0

标准库类型赋值运算符的引用限定符

我想知道,标准类型的赋值运算符没有左值引用限定是否有原因?他们都不是。 因此,我们可以这样写: std::string{} = "42"...

回答 2 投票 0

如何使用相同的无名 lambda 处理多个范围?

我有一些无名的 lambda 函数,我需要将其应用于许多范围,并且不喜欢重复它以避免代码重复(它由 5 行以上代码组成)。 最简单的实施方法是什么...

回答 2 投票 0

如何检查可调用对象是否具有特定的签名,包括参数和返回类型?

我有两个函数系列,它们以两种不同的方式更新变量: 调用一些函数来进行一些更改,然后客户端代码应应用这些更改(在下面的代码中计算);

回答 1 投票 0

如何将 std::views::filter 结果用于 std::ranges::random_access_range?

std::views::filter 无法模拟 std::ranges::random_access_range 概念(很可能是因为它无法将临时过滤范围的随机访问映射到原始范围;如果我是 w,请纠正我。 ..

回答 1 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.