我如何将一个元素插入一个值,该元素是多映射C++
我在我的set< values_class>
中添加新值时遇到了麻烦,该值在我的map< key中,set< values_class>>
。当我尝试添加这样的值时:
Key key(get<0>(key_args), get<1>(key_args));
if (!(get<0>(key_args) < 1 && get<0>(key_args) > m_existing_keys_set.size()) && m_existing_keys_set.find(key) != m_existing_keys_set.end())
{
Value_Class value(get<0>(value_args), get<1>(value_args), get<2>(value_args));
set<Value_Class> _temp_set{ value };
m_map_with_elements.emplace(move(key), move(_temp_set));
}
仅我在地图中添加的第一个值。其他值只是不插入,总是只有第一个添加值。您能帮我,告诉我应该如何解决问题吗?整个代码:
#pragma once
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <map>
#include <set>
using namespace std;
class Main_Class
{
private: //private section for classes and some helper functions
class Value_Class; struct Key;
set<Key> m_existing_keys_set
{
{1, "Key"}
}; // all the existing keys will be stored here
/*----------------------classes start space------------------*/
///
/// Value_Class
///
class Value_Class
{
public:
private:
string m_value_name;
fouble m_value_double_data;
bool m_is_available;
public:
Value_Class(string val_name, double val_double_data, bool is_avialable) : //Values_Class Constructor
m_value_name(move(val_name)),
m_value_double_data(val_double_data),
m_is_available(is_avialable) {};
~Value_Class() {};
friend ostream& operator<<(ostream& os, const Value_Class& dish) {// Overloading the output << operator
return os << dish.m_value_name << " >-=-=-=-< Price: " << dish.m_value_double_data;
}
bool operator<(const Value_Class& dish) const {// Overloading the < operator
return tie(m_value_name, m_value_double_data, m_is_available) < tie(dish.m_value_name, dish.m_value_double_data, dish.m_is_available);
}
};
///
/// KEY
///
struct Key
{
typedef int dish_number_category_t;
typedef string dish_name_category_t;
private:
dish_number_category_t m_key_number;
dish_name_category_t m_key_name;
public:
Key(dish_number_category_t num, dish_name_category_t str) : // Key constructor
m_key_number(num),
m_key_name(move(str)) {};
bool operator<(const Key& rhs) const {
return tie(m_key_number, m_key_name) < tie(rhs.m_key_number, rhs.m_key_name);
}
friend ostream& operator<<(ostream& os, const Key& k) {
return os << k.m_key_name;
}
bool operator==(const Key& _key) const
{
return tie(_key.m_key_number, _key.m_key_name) == tie(m_key_number, m_key_name);
}
};
/*----------------------classes end space------------------*/
template <class... Args1, class... Args2>
void add_helper(tuple<Args1...> key_args,
tuple<Args2...> value_args) // Helper method
{
Key key(get<0>(key_args), get<1>(key_args));
if (!(get<0>(key_args) < 1 && get<0>(key_args) > m_existing_keys_set.size()) && m_existing_keys_set.find(key) != m_existing_keys_set.end())
{
Value_Class value(get<0>(value_args), get<1>(value_args), get<2>(value_args));
set<Value_Class> _temp_set{ value };
m_map_with_elements.emplace(move(key), move(_temp_set));
}
}
public: static Main_Class* get_instance();
private: // private section for all singleton stuff
Main_Class() {};
Main_Class(const Main_Class&) = delete;
Main_Class operator=(const Main_Class&) = delete;
static Main_Class* _instance; // The only object og the Maim_Class
map<Key, set<Value_Class>> m_map_with_elements; // the map
public: // public section for all the public functions
template <class... Args>
void add_new_element_to_a_map(Args&&... args)
{
add_helper(forward<Args>(args)...);
}
void print_map() const
{
for (auto& itr : m_map_with_elements)
{
cout << "Key: " << itr.first << endl;
for (auto& itr2 : itr.second)
cout << itr2 << endl;
}
}
};
Main_Class* Main_Class::_instance = 0;
Main_Class* Main_Class::get_instance()
{
if (_instance == 0)
_instance = new Main_Class();
return _instance;
} //Singleton access function
预先感谢
I have a trouble with adding a new values to a set<Value_Class>
that is in my map<Key, set<Values_Class>>
. When I try to add a value like this:
Key key(get<0>(key_args), get<1>(key_args));
if (!(get<0>(key_args) < 1 && get<0>(key_args) > m_existing_keys_set.size()) && m_existing_keys_set.find(key) != m_existing_keys_set.end())
{
Value_Class value(get<0>(value_args), get<1>(value_args), get<2>(value_args));
set<Value_Class> _temp_set{ value };
m_map_with_elements.emplace(move(key), move(_temp_set));
}
There's only the first value I added in the map. The other values just don't insert, there's always only the first added value. Can You help me and tell how should I solve a problem? The whole code:
#pragma once
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <map>
#include <set>
using namespace std;
class Main_Class
{
private: //private section for classes and some helper functions
class Value_Class; struct Key;
set<Key> m_existing_keys_set
{
{1, "Key"}
}; // all the existing keys will be stored here
/*----------------------classes start space------------------*/
///
/// Value_Class
///
class Value_Class
{
public:
private:
string m_value_name;
fouble m_value_double_data;
bool m_is_available;
public:
Value_Class(string val_name, double val_double_data, bool is_avialable) : //Values_Class Constructor
m_value_name(move(val_name)),
m_value_double_data(val_double_data),
m_is_available(is_avialable) {};
~Value_Class() {};
friend ostream& operator<<(ostream& os, const Value_Class& dish) {// Overloading the output << operator
return os << dish.m_value_name << " >-=-=-=-< Price: " << dish.m_value_double_data;
}
bool operator<(const Value_Class& dish) const {// Overloading the < operator
return tie(m_value_name, m_value_double_data, m_is_available) < tie(dish.m_value_name, dish.m_value_double_data, dish.m_is_available);
}
};
///
/// KEY
///
struct Key
{
typedef int dish_number_category_t;
typedef string dish_name_category_t;
private:
dish_number_category_t m_key_number;
dish_name_category_t m_key_name;
public:
Key(dish_number_category_t num, dish_name_category_t str) : // Key constructor
m_key_number(num),
m_key_name(move(str)) {};
bool operator<(const Key& rhs) const {
return tie(m_key_number, m_key_name) < tie(rhs.m_key_number, rhs.m_key_name);
}
friend ostream& operator<<(ostream& os, const Key& k) {
return os << k.m_key_name;
}
bool operator==(const Key& _key) const
{
return tie(_key.m_key_number, _key.m_key_name) == tie(m_key_number, m_key_name);
}
};
/*----------------------classes end space------------------*/
template <class... Args1, class... Args2>
void add_helper(tuple<Args1...> key_args,
tuple<Args2...> value_args) // Helper method
{
Key key(get<0>(key_args), get<1>(key_args));
if (!(get<0>(key_args) < 1 && get<0>(key_args) > m_existing_keys_set.size()) && m_existing_keys_set.find(key) != m_existing_keys_set.end())
{
Value_Class value(get<0>(value_args), get<1>(value_args), get<2>(value_args));
set<Value_Class> _temp_set{ value };
m_map_with_elements.emplace(move(key), move(_temp_set));
}
}
public: static Main_Class* get_instance();
private: // private section for all singleton stuff
Main_Class() {};
Main_Class(const Main_Class&) = delete;
Main_Class operator=(const Main_Class&) = delete;
static Main_Class* _instance; // The only object og the Maim_Class
map<Key, set<Value_Class>> m_map_with_elements; // the map
public: // public section for all the public functions
template <class... Args>
void add_new_element_to_a_map(Args&&... args)
{
add_helper(forward<Args>(args)...);
}
void print_map() const
{
for (auto& itr : m_map_with_elements)
{
cout << "Key: " << itr.first << endl;
for (auto& itr2 : itr.second)
cout << itr2 << endl;
}
}
};
Main_Class* Main_Class::_instance = 0;
Main_Class* Main_Class::get_instance()
{
if (_instance == 0)
_instance = new Main_Class();
return _instance;
} //Singleton access function
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您假设
std :: map :: emplace()
将有效地照顾关键重复,而文档表示无法保证它。查看 std :: map :: map :: emplace()
您可以使用
std :: map :: erase()
要删除该项目的存在,那么您将获得预期的行为。您还可以提供自己的手工制作功能来处理此功能。
You've assumed that
std::map::emplace()
will effectively take care of the key duplication while the documentation said that it's not guaranteed.check out std::map::emplace()
You can use
std::map::erase()
to erase the item if it exists, then you will get the expected behavior.You can also provide your own hand-crafted function to handle this.