
[英]Best practice for overloading functions with only STL types
我想重載operator<<(std::ostream&, std::chrono::duration<std::uint64, std::nano>) - 理想情況下不需要用戶在任何地方編寫using語句。 這里的最佳做法是什么? 語境... 有沒有一種方法可以在不 ...
[英]Best practice for overloading functions with only STL types
我想重載operator<<(std::ostream&, std::chrono::duration<std::uint64, std::nano>) - 理想情況下不需要用戶在任何地方編寫using語句。 這里的最佳做法是什么? 語境... 有沒有一種方法可以在不 ...
[英]c++ Overloaded operator () change std::set.find()
我正在學習 std::set。 這是我的代碼: Output: 1 我使用 struct cmp 來自定義元素的排序規則,如果 abs(ab)<=3 新元素將被刪除。 但是讓我吃驚的是 q.find() 已經改變了。 我想知道為什么output是1,q里面沒有4。 q.find(4) 是 ...
[英]How can I approach this CP task?
任務(來自保加利亞法官,點擊“Език”將其更改為英語): 我得到了 N 種珊瑚中第一個 (S 1 = A) 的大小。 每個后續珊瑚的大小(S i ,其中 i > 1)使用公式 (B*S i-1 + C)%D 計算,其中 A、B、C 和 D 是一些常數。 我被告知 Nemo 在第 K個珊瑚附 ...
[英]Is there a way to pass variables to std::make_tuple<double, char, std::string>(var1, var2)?
我想將變量名傳遞給 std::make_tuple(),但它不允許。 我正在使用 C++14,有沒有辦法實現我想要的? 這會拋出一條錯誤消息“無法將‘int&&’類型的右值引用綁定到‘int’類型的左值” ...
[英]!st.empty() is throwing runtime error if kept after &&
while(.st.empty() && nums[i%n] >= st.top()) st;pop(); 這段代碼有效但是while(nums[i%n] >= st.top() &&.st.empty()) st;pop(); 這不是! 我正在解決 le ...
[英]Is it possible to create an STL container where the contents are mutable, but the container's attributes aren't?
這可能最容易用例子來解釋。std::vector<int> a; a.push_back(1); a[0] = 2; const std::vector<int>& b = a; b.push_back(1); // Throws an error I want. ...
[英]Finding a key-value-pair within a std::multimap with key of type int
我有一個std::multimap<int, X*> ,其中X是用戶定義的類型。 我想在此多重映射中找到特定的鍵值對(即指向此對的迭代器)。 (A) 完整示例: 但是,這不會編譯(帶有-std=gnu++2a的 gcc 12.2.1): 所以在我看來int以某種方式轉換為const i ...
[英]segmentation fault after popping the element that has been pointed to by
我發現這段代碼可能會導致分段錯誤。 因為ptr最終指向彈出的元素。 但我不知道發生這種情況的確切原因。 我只是假設pop操作在內部為彈出的 memory 解除分配。 我的推測對嗎? 還是其他原因? ...
[英]Custom C++ Allocator cause error in : _Container_base12::_Orphan_all_unlocked_v3()
所以我打算將這個基於堆棧的分配器用於 std::vector,並且我使用 2 arrays 進行分配(因為向量增長並將舊緩沖區復制到新緩沖區)。 這是完整的代碼: 代碼以某種方式導致崩潰:在此處輸入圖像描述在此處輸入圖像描述我注意到“_Container_base12”繼承自分配器,所以我想我的“ ...
[英]Iterate over all elements with key k in a std::unordered_multimap in O(count(k))
我有一個std::unordered_multimap ,並且想遍歷具有給定鍵 k 的所有元素,而不遍歷完整的 map,但最好只遍歷匹配的項目。 雖然我可以在有序的std::multimap中使用 upper_bound 來做到這一點,但我無法在任何地方找到find()后跟前向迭代直到鍵不同,將遍歷 ...
[英]STL functions not working in vscode for my Mac
該圖顯示了我目前正在使用 MacOS Ventura 13.1 的 push()、pop()、empty() 函數中顯示的錯誤,如果有人能提供幫助,那就太好了。 由於這些功能不起作用,我無法將 STL 模板用於我的代碼。 ...
[英]How to pass a template type (std::array or std::vector) to a template parameter
我有一個 function 模板,其中 function 的返回類型是模板參數。 根據https://en.cppreference.com/w/cpp/language/template_argument_deduction ,我們可以通過以下示例進行模板參數推導template<typen ...
[英]Is std::unordered_map thread safe in this case?
假設一個線程只連續調用以下函數。 此處, insert_data檢查std::unordered_map中是否存在鍵,如果不存在, insert_data調用 function 添加新鍵並修改其值。void insert_data(int key, int value, std::unordere ...
[英]Find max/min value of a member class in a container
我有一個類的向量/容器,我想找到具有特定成員的最大/最小值的元素; 例如: 是否有一種“STL”方法可以在不顯式遍歷容器並比較值的情況下找到項目? 對兩種解決方案的評論我將在此處添加一個基於 @Jarod42 對已接受解決方案的評論的解決方案,該解決方案基於 std::ranges::less{}: ...
[英]Getting build error when trying to implement a comparator for sorting map data type?
我正在嘗試為 map 構建一個比較器,以便與 sort() function 一起使用。考慮一個 map object 被稱為 right,其 id 和坐標作為基本成員。 我正在嘗試用歐式距離對這個 object 的元素進行排序。 下面是代碼。 構建時出現以下錯誤 我真的很感激你能提供的任何幫助。 ...
[英]Which std::string constructor am I using?
我看到 std::string 有很多構造函數https://cplusplus.com/reference/string/string/string/ 。 但是當我這樣做時我使用的是哪一個: 我打賭是: 來自 c-string (4) string (const char* s); (4) 來 ...
[英]Why I can't insert the pair value like this? v.push_back(vector<pair<int,string> >({1,"A"}));?
我已經寫了這段代碼,想像這樣在 vector of vector 中插入值: v.push_back(vector<pair<int,string> >({1,"A"})); ?? 但是它顯示錯誤...... 在此代碼中插入空值有效。 但我想知道為什么這不可能?? 我想在 ...
[英]Why is there a "erase–remove idiom" for std::remove, std::remove_if with containers?
我只是在研究為什么函數std::remove_if沒有按我預期的方式工作,並了解了 C++ “erase-remove idiom”,我應該在其中傳遞remove或remove_if的結果以erase以實際刪除我想要從容器中取出的物品。 這讓我覺得很不直觀:這意味着remove和remove_if ...
[英]Getting compiler error: Request for member push in pq, which is of non-class type std::priority_queue
我收到以下錯誤: :60:8: 錯誤:請求'pq'中的成員'push',它是非類類型'std::priority_queue, std::vector >, std::function, std::pair)> >(比較器)' 60 | pq.push(p1); 我的代碼如下 ...
[英]Is std::array iterator typedef implementation defined
std::array<T,N>::iterator 實現的迭代器類型要求是已定義還是始終定義為指向 value_type 的指針? ...