【问题标题】:Creating instances for a linked list class in C++在 C++ 中为链表类创建实例
【发布时间】:2018-10-21 07:31:18
【问题描述】:

我目前正在学习 C++ 链表,我从教科书中点击了这段代码。我无法理解:

const string& e

我正在尝试在我的主函数中为这个类编写一些实例,以查看事情是如何工作的,但不知道如何。 例如,我想在列表中添加 3、5、7 并在列表的前面添加 1,而不是从列表中删除 7。

#include <cstdlib>
#include <iostream>
#include <string>

using std::string;
using namespace std;



class StringNode {                      // a node in a list of strings
private:
  string elem;                              // element value
  StringNode* next;                     // next item in the list

  friend class StringLinkedList;                // provide StringLinkedList 
                                                // access
};

class StringLinkedList {                    // a linked list of strings
public:
  StringLinkedList();                       // empty list constructor
  ~StringLinkedList();                          // destructor
  bool empty() const;                       // is list empty?
  const string& front() const;                      // get front element
  void addFront(const string& e);               // add to front of list
  void removeFront();                       // remove front item list
private:
  StringNode* head;                     // pointer to the head of list
};

StringLinkedList::StringLinkedList()            // constructor
  : head(NULL) { }

StringLinkedList::~StringLinkedList()           // destructor
  { while (!empty()) removeFront(); }

bool StringLinkedList::empty() const            // is list empty?
  { return head == NULL; }

const string& StringLinkedList::front() const       // get front element
  { return head->elem; }



void StringLinkedList::addFront(const string& e) {  // add to front of list
  StringNode* v = new StringNode;           // create new node
  v->elem = e;                          // store data
  v->next = head;                   // head now follows v
  head = v;                     // v is now the head
}



void StringLinkedList::removeFront() {              // remove front item
  StringNode* old = head;               // save current head
  head = old->next;                 // skip over old head
  delete old;                       // delete the old head
}

【问题讨论】:

  • const string &amp;e 是对常量(只读)字符串的引用。它用于例如void addFront(const string&amp; e); 提供对现有字符串的引用(仅可用于读取访问),而无需复制字符串(内容)本身。 (这不会阻止 addFront() 从 e 复制到 v-&gt;elem。)

标签: c++ class linked-list


【解决方案1】:

我试图寻找解释 C++ 如何使用按值调用的副本。

在 C++ 中,T 类型的函数参数将在调用函数之前制作对象的副本。

int myFunction( int value )
{
     value = value + 1;
}

int main( int argc, char * argv[] )
{
    int elem = 6;
    myFunction( elem );
    printf( "%d\n", elem ); // elem = 6;
}

在上面的例子中,int 值的副本被发送到myFunction,并且副本递增。

这可能不是我们想要的,我们可以通过修改myFunction 来获取值的引用,将结果更改为 7。这是通过使用“&”来描述参考值来完成的。

int myFunction( int & value )
{
     value = value + 1;
}

int main( int argc, char * argv[] )
{
    int elem = 6;
    myFunction( elem );
    printf( "%d\n", elem ); // elem = 7;
}

在上述情况下,没有复制,并且 elem 得到更新。

将引用传递给函数有两个主要原因

  1. 允许更新值(或对象)。
  2. 为了避免复制值的成本。

在您引用的示例中,第二种情况是使用 const string &amp; 的原因(对字符串对象的引用)。 std::string,构造和销毁都有成本,所以通过发送引用来避免这种情况,效率更高。

为了补充这种用法,引用通常是const,以使编译器相信不应更改该值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2012-01-05
    相关资源
    最近更新 更多