std 相关问题

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

C++ 编译器支持 std::execution(并行 STL 算法)

我想使用 std::sort 的并行版本,我可以在其中指定 std::execution::par_unseq 等执行策略。 我目前在 Ubuntu Linux 下使用 clang++-10 和 g++ 7.5.0,但两者都...

回答 2 投票 0

范围支持的算法实现

查看像 std::ranges::upper_bound 这样的算法,我看到两种实现: 对于迭代器和哨兵表示的范围 对于直接由范围表示的范围 这是一种遗产还是

回答 1 投票 0

如何在std::wstringstream中插入NULL字符?

我需要根据 std::vector 中表示的值列表在 C++ 中构造一个多字符串 (REG_MULTI_SZ)。 这是首先想到的,但当然是错误的: std::ws...

回答 1 投票 0

如何一次性包含所有 C++ 标准库?

我正在使用向量和链表进行一个班级项目。但在 C++ 中,为了应用它们,我需要在标头中包含以下代码。 #包括 #包括 我知道

回答 8 投票 0

std::erase() 和 std::erase_if() 是否会导致向量的重新分配?

正如问题标题所述,std::erase() 和 std::erase_if() 是否会导致向量的重新分配? 两者中哪一个更有效:我应该使用擦除删除惯用语还是...

回答 1 投票 0

为什么 std::swap<std::array<int,3>> 无法编译?

我想交换两个固定大小的整数数组。 与直觉相反,以下内容无法编译,因为没有找到匹配的交换实例。 #包括 #包括<

回答 2 投票 0

视觉工作室中有状态分配器?看起来有点坏了 - 有没有办法在 VS 中编写一个有效的有状态自定义分配器?

将某些代码移植到 VS 不起作用,因为 std::allocator 的实现不能正确支持具有状态的分配器。 从在 gcc 和 clang 上运行良好的代码示例来看,它看起来...

回答 1 投票 0

以 std::pair 作为键透明搜索 std::map

如果有 std::map, some_type> 查找其值的最佳方法是什么? 我想最明显的就是做这样的事情: 地图.find(std::

回答 2 投票 0

通过指针传递 C++ 向量时可以更改地址

如果我有类似下面的代码,我传递一个向量指针(例如它可以为空),然后在被调用的函数中我向向量添加项,是否存在我的向量指针变成的危险

回答 1 投票 0

如何在gdb中调用std库函数(c++)?

我想使用gdb(使用版本12.1)来调试c++程序(g++版本11.3)。最小可重现示例如下: // foo.cpp #包括 #包括 #在...

回答 1 投票 0

如何查找给定的键是否存在于 std::map 中

我正在尝试检查给定的键是否在地图中,但有些无法做到: typedef 映射::迭代器 mi; 地图米; m.insert(make_pair("f","++--")); 一对 我正在尝试检查给定的键是否在地图中,但有些做不到: typedef map<string,string>::iterator mi; map<string, string> m; m.insert(make_pair("f","++--")); pair<mi,mi> p = m.equal_range("f");//I'm not sure if equal_range does what I want cout << p.first;//I'm getting error here 那么我怎样才能打印p中的内容呢? 使用 map::find 和 map::end: if (m.find("f") == m.end()) { // not found } else { // found } 要检查映射中是否存在特定键,请通过以下方式之一使用 count 成员函数: m.count(key) > 0 m.count(key) == 1 m.count(key) != 0 map::find的文档说:“另一个成员函数map::count可用于仅检查特定键是否存在。” map::count的文档说:“因为地图容器中的所有元素都是唯一的,所以该函数只能返回1(如果找到该元素)或零(否则)。” 要通过您知道存在的键从映射中检索值,请使用 map::at: value = m.at(key) 与 map::operator[] 不同,如果指定的键不存在,map::at 不会在映射中创建新键。 C++20 为我们提供了 std::map::contains 来做到这一点。 #include <iostream> #include <string> #include <map> int main() { std::map<int, std::string> example = {{1, "One"}, {2, "Two"}, {3, "Three"}, {42, "Don\'t Panic!!!"}}; if(example.contains(42)) { std::cout << "Found\n"; } else { std::cout << "Not found\n"; } } 您可以使用.find(): map<string,string>::iterator i = m.find("f"); if (i == m.end()) { /* Not found */ } else { /* Found, i->first is f, i->second is ++-- */ } C++17 通过带有初始化器的 If 语句进一步简化了这一点。 这样你就可以鱼与熊掌兼得了。 if ( auto it{ m.find( "key" ) }; it != std::end( m ) ) { // Use `structured binding` to get the key // and value. const auto&[ key, value ] { *it }; // Grab either the key or value stored in the pair. // The key is stored in the 'first' variable and // the 'value' is stored in the second. const auto& mkey{ it->first }; const auto& mvalue{ it->second }; // That or just grab the entire pair pointed // to by the iterator. const auto& pair{ *it }; } else { // Key was not found.. } m.find == m.end() // not found 如果您想使用其他API,请找到m.count(c)>0 if (m.count("f")>0) cout << " is an element of m.\n"; else cout << " is not an element of m.\n"; 我想你想要map::find。如果 m.find("f") 等于 m.end(),则未找到密钥。否则,find 返回一个指向找到的元素的迭代器。 错误是因为p.first是一个迭代器,它不适用于流插入。将最后一行更改为 cout << (p.first)->first;。 p 是一对迭代器,p.first 是迭代器,p.first->first 是键字符串。 一张地图对于给定的键只能有一个元素,所以 equal_range 不是很有用。它是为映射定义的,因为它是为所有关联容器定义的,但它对于多重映射更有趣。 template <typename T, typename Key> bool key_exists(const T& container, const Key& key) { return (container.find(key) != std::end(container)); } 当然,如果你想变得更奇特,你可以随时模板化一个函数,该函数也采用已找到的函数和未找到的函数,如下所示: template <typename T, typename Key, typename FoundFunction, typename NotFoundFunction> void find_and_execute(const T& container, const Key& key, FoundFunction found_function, NotFoundFunction not_found_function) { auto& it = container.find(key); if (it != std::end(container)) { found_function(key, it->second); } else { not_found_function(key); } } 并像这样使用它: std::map<int, int> some_map; find_and_execute(some_map, 1, [](int key, int value){ std::cout << "key " << key << " found, value: " << value << std::endl; }, [](int key){ std::cout << "key " << key << " not found" << std::endl; }); 这样做的缺点是想出一个好名字,“find_and_execute”很尴尬,我想不出更好的名字...... map<string, string> m; 检查 key 是否存在,并返回出现次数(map 中为 0/1): int num = m.count("f"); if (num>0) { //found } else { // not found } 检查key是否存在,并返回迭代器: map<string,string>::iterator mi = m.find("f"); if(mi != m.end()) { //found //do something to mi. } else { // not found } 在你的问题中,由坏的operator<<过载引起的错误,因为p.first是map<string, string>,你无法打印出来。尝试这个: if(p.first != p.second) { cout << p.first->first << " " << p.first->second << endl; } 小心地将查找结果与地图“m”的结尾进行比较,因为所有答案都有 上面完成 地图::迭代器 i = m.find("f"); if (i == m.end()) { } else { } 您不应该尝试执行任何操作,例如如果迭代器 i 等于 m.end() 则打印键或值,否则会导致分段错误。 比较 std::map::find 和 std::map::count 的代码,我认为第一个可能会产生一些性能优势: const_iterator find(const key_type& _Keyval) const { // find an element in nonmutable sequence that matches _Keyval const_iterator _Where = lower_bound(_Keyval); // Here one looks only for lower bound return (_Where == end() || _DEBUG_LT_PRED(this->_Getcomp(), _Keyval, this->_Key(_Where._Mynode())) ? end() : _Where); } size_type count(const key_type& _Keyval) const { // count all elements that match _Keyval _Paircc _Ans = equal_range(_Keyval); // Here both lower and upper bounds are to be found, which is presumably slower. size_type _Num = 0; _Distance(_Ans.first, _Ans.second, _Num); return (_Num); } find() 和 contains() 都可以使用。根据文档。两种方法平均时间为常数,最坏情况下为线性时间。 我知道这个问题已经有一些很好的答案,但我认为我的解决方案值得分享。 它适用于 std::map 和 std::vector<std::pair<T, U>>,并且可从 C++11 开始使用。 template <typename ForwardIterator, typename Key> bool contains_key(ForwardIterator first, ForwardIterator last, Key const key) { using ValueType = typename std::iterator_traits<ForwardIterator>::value_type; auto search_result = std::find_if( first, last, [&key](ValueType const& item) { return item.first == key; } ); if (search_result == last) { return false; } else { return true; } } map <int , char>::iterator itr; for(itr = MyMap.begin() ; itr!= MyMap.end() ; itr++) { if (itr->second == 'c') { cout<<itr->first<<endl; } } 如果你想比较成对的地图,你可以使用这个方法: typedef map<double, double> TestMap; TestMap testMap; pair<map<double,double>::iterator,bool> controlMapValues; controlMapValues= testMap.insert(std::pair<double,double>(x,y)); if (controlMapValues.second == false ) { TestMap::iterator it; it = testMap.find(x); if (it->second == y) { cout<<"Given value is already exist in Map"<<endl; } } 这是一项有用的技术。

回答 15 投票 0

有没有一种使用STL查找容器中最大元素的便捷方法?

有没有办法使用STL找到容器内最大的容器? ATM机,我有这个 相当幼稚的做法: int main() { std::vector > v; ......

回答 3 投票 0

通过外部评估调度将标准兼容性与不透明数据相连接

我正在低级别接收托管数据 { void * data; uint 步幅,计数; } 格式。我可以读取、写入和交换数据项,但不能添加或删除/调整大小/重新分配。有足够的信息...

回答 1 投票 0

filesystem::copy 返回无效参数,但参数是现有的 fs::path 变量

我正在使用 std::filesystem (fs) 库处理项目中的文件。我存储为 fs::path 变量的所有路径。我的代码是: int main(){ std::filesystem::path root_dir = "/home/xyz/study/

回答 1 投票 0

使用C++仅打印前几行数据

我目前正在做一个项目,我需要绘制烛台数据。但是,我的输出给出了导致我无法向上滚动到标题的所有数据。有什么办法可以让我...

回答 1 投票 0

为什么我不能在 MinGW 12.2.0 和 C++11 上使用 std::atanf?

从规范https://en.cppreference.com/w/cpp/numeric/math/atan来看,C++11上似乎存在std::atanf,但在godbolt上它说“atanf”不是成员“标准”的: #包括 #包括 从规范https://en.cppreference.com/w/cpp/numeric/math/atan来看,似乎std::atanf存在于C++11上,但在godbolt上它说“atanf”不是“std”成员: #include <iostream> #include <cmath> int main() { float sampleOverLoaded = 200.0f; float outputAtan = atanf(sampleOverLoaded); float outputAtanStd = std::atanf(sampleOverLoaded); std::cout << outputAtan << std::endl; std::cout << outputAtanStd << std::endl; } 怎么了?请注意,我不想使用 atanhf。 这是一个长期存在的错误,已于 2023 年 11 月得到解决。请参阅 GCC Bug 79700 - std::fabsf 和 std::fabsl 中缺少 <cmath> 。 std::atanf 位于 C++11 标准中,但在过去 12 年左右的时间里没有人愿意添加它。有些人还认为它不应该存在(请参阅链接的错误以获取更多讨论)。 暂时使用std::atan。 这是一个重载函数,接受 float、double 和 long double: float atan ( float num ); double atan ( double num ); long double atan ( long double num ); // (until C++23) /* floating-point-type */ atan ( /* floating-point-type */ num ); // (since C++23)

回答 1 投票 0

如何获取两个字符串向量之间不同或共同元素的数量?

是否有一个函数可以比较两个字符串向量以返回不同(或相同)元素的数量?我是否必须迭代它们并逐项测试?

回答 4 投票 0

为什么我无法在 const std::map 中使用运算符[]访问元素?

我尝试使用operator[]访问const映射中的元素,但是这个方法失败了。我也尝试使用 at() 来做同样的事情。这次成功了。但是,我找不到任何参考...

回答 4 投票 0

为什么重载解析在显式提供模板参数时会选择错误的重载?

我的代码在这里: #包括 #包括 #包括 模板 [[也许未使用]] constexpr auto t1(const std::queue &val...

回答 1 投票 0

为什么这个const类型的模板会出错?

感谢阅读这个问题。 代码在这里。版本 C++17 #包括 #包括 #包括 模板 [[也许未使用]] constexpr 自动 t1(

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.