C++指针未更新值

C++ Pointer not updating the value

本文关键字:更新 指针 C++      更新时间:2023-10-16
//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;
  }
}