C++指针未更新值
C++ Pointer not updating the value
//Inside the header.h
class telefonbok{
std::map<std::string,std::string> telebok;
std::map<std::string,std::string> aliasbok;
}
//Inside the cpp file
void telefonbok::alias(string name, string alias){
if (telebok.find(name) == telebok.end()) {
cout << "Not found" << endl;
} else {
//pointer = Adress stored in pointer.
//*pointer = Value of *pointer
string *pointer;
pointer = &telebok.find(name)->second;
aliasbok.insert(make_pair(alias, *pointer));
cout << *pointer << endl;
}
当我更改第一个地图(telebok(中的值时,地图(别名(中的第二个值保持不变(应该将指针作为值的值(。
例:
添加彼得 123
查找彼得:
彼得 : 123
别名 彼得·皮特
查找皮特:
皮特 : 123
改变彼得 987
查找彼得:
彼得 : 987
查找皮特:
皮特 : 123
(皮特从不改变,这是问题所在,它应该始终具有与彼得相同的价值(
首先,您的代码效率低下(您调用 find 两次,这是非常昂贵的操作(,因此您应该使用迭代器,而不是指针,这也将有助于解决您的问题:
class telefonbok{
typedef std::map<std::string,std::string> Telebok;
typedef std::map<std::string,Telebok::const_iterator> Aliasbok;
Telebok telebok;
Aliasbok aliasbok;
};
void telefonbok::alias(string name, string alias)
{
Telebok::const_iterator f = telebok.find( name );
if( f == telebok.end() )
cout << "Not found" << endl;
else
aliasbok.insert( make_pair( alias, f );
}
现在您有以下内容:
- 你不给
std::map::find()
打电话两次 - 通过别名,您不仅可以找到数字,还可以找到原始名称
要从别名代码打印值稍微复杂一点:
for( Aliasbok::const_iterator it = aliasbok.begin(); it != aliasbok.end(); ++it )
cout << "alias " << it->first << " has number " << it->second->second << " original name " << it->second->first << endl;
或者查找值:
Aliasbok::const_iterator it = aliasbok.find( "abcd" );
if( it != aliasbok.end() )
cout << "phone for " << it->first << " is " << it->second->second << endl;
else
cout << "phone for abcd not found" << endl;
但是使用迭代器时应该习惯它
注意:如果从电话簿中删除记录,则需要先清理别名簿,否则它将包含无效的迭代器。正确的解决方案是使用boost::multi_index
但对于您的水平来说可能太复杂了。
当我更改第一个地图(telebok(中的值时,地图(别名(中的第二个值保持不变(应该将指针作为值的值(。
如果aliasbok
应该将指针作为值,那么您的错误是您错误地定义了地图。这是您的定义:
std::map<std::string,std::string> aliasbok;
请注意映射的值类型如何std::string
哪个是值类型,而不是哪个是指针类型std::string*
。
还要注意这一行:
aliasbok.insert(make_pair(alias, *pointer));
取消引用指针并将指向的字符串复制到对中,而不是复制指针。
您必须存储共享指针并将它们适当地设置为同一字符串。
class telefonbok{
std::map<std::string,std::shared_ptr<std::string>> telebok;
std::map<std::string,std::shared_ptr<std::string>> aliasbok;
}
并根据需要提供更新指针的方法,例如
void addphone(std::string const & name,
std::string const & alias,
std::string const & phone);
{
std::shared_ptr<std::string> phone_ptr(new std::string(phone));
telefonbok.telebok.emplace(name,phone_ptr);
telefonbok.aliasbok.emplace(alias,phone_ptr);
}
这样,当您修改任一手机时,它将在两者上更新。当您退出功能范围时,您的手机也不会死机。
然后,您可以执行此操作以进行更新
void update_phone(std::string const & name,
std::string const & newphone)
{
if( auto iter = telefonbok.telebok.find(name) )
{
*(iter->second.get()) = newphone;
} else if ( auto iter = telefonbok.aliasbok.find(name) )
{
*(iter->second.get()) = newphone;
}
}
相关文章:
- 指针没有更新它在void函数内部指向的值
- C++:添加新结构时,结构指针向量中的所有元素都会更新
- 在删除之前更新一组元素指针中的元素
- C++ - 更新指针中的值被覆盖
- 在这种情况下,当尝试从单独的类更新变量时,我是否需要使用指针?
- 当矢量改变容量时,有什么方法可以更新指针/参考值
- 在构造函数中更新指针向量时出现问题
- C++ 指针问题 - 通过方法更新指针
- 是否可以使用一系列智能指针来自动通过其索引更新其值
- 优先级队列实现为单一链接,无法更新插入的指针转换
- 线程更新全局指针时对全局指针的影响
- 用类方法更新指针的困难
- 通过指针更新矢量的元素
- 使用指针对指针可以解决类成员不更新的问题吗?如果是,如何
- 指向节点的指针不更新节点属性
- 在函数之间传递时指针未更新
- 已更新:从函数返回指向数组的指针
- 使用引用更新智能指针
- 如果通过指针分配,C++ vector.size() 不会更新
- C++指针未更新值