小编use*_*120的帖子

从端到端遍历(C++ LL Q:1)


int LinkedList::DoStuff()
{
Node *Current    = next_;
while ( Current != NULL )
    {
        Current = Current->next_;
        length_++;
    }
    // At the last iteration we have reached the end/tail/last node
    return length_;
}
Run Code Online (Sandbox Code Playgroud)

除了最后一个节点之外没有其他节点.我怎样才能穿过前端的尾端?

c++ linked-list

1
推荐指数
1
解决办法
2735
查看次数

星期二+ 3 =星期五?C++编程问题

看一下main函数,我们可以看到我已经将"星期一"硬编码到我的setDay公共函数中.使用c-string(就像我在setDay中所做的那样)很容易从用户那里获取一周中的某一天,但是我如何要求用户将n添加到设置的日期,"星期一"并提出"星期四"?这很难,因为typdef枚举{无效,星期一,星期二,星期三,星期四,星期五,星期六,星期日}不解释9是0和/或10为1.

#include <iostream>

using std::cout;
using std::endl;

class DayOfTheWeek //class is encapsulation of functions and members that manipulate the data.
{
public:

 DayOfTheWeek(); // Constructor
 virtual ~DayOfTheWeek(); // Destructor

 void setDay(const char * day); // Function to set the day
 void printDay() const;      // Function to Print the day.
 const char * getDay() const;   // Function to get the day.

 const char * plusOneDay();  // Next day function
 const char * minusOneDay(); // Previous day function
 const char * addDays(int …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
2
解决办法
1967
查看次数

可以在不读取文件两次的情况下完成吗?C++

我在main.cpp中做的是读取文本文件两次:一次从每个路径名中删除'\',然后再次将文件的原始名称提供给我的实现文件中的SetPath().

// This a read file sub-routine called in the main function.
// Its purpose was intentionally set out to read the file, and pass information
// to functions to MyClassFile Implementational file.


// global varaible to have the file spec.
char MyClass::List_[ ARRY_SZ ] = {};

int main()
{

..
inFile.open( MyClass::List_, ios::in  );

    while ( inFile.peek() != '\n' )
    {
        inFile.get( ch );
        if ( ch == 92 )
            count++;
    }

    inFile.clear();
    inFile.close();

    inFile2.open( MyClass::List_, ios::in  );
    while …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
317
查看次数

堆腐败,可能的内存泄漏,C + +

我有一份我差不多完成的家庭作业.

由于效率低下,我只是想知道如何在程序结束时防止崩溃.

quack::quack(int capacity) : backPtr( NULL ), frontPtr( NULL )
{
    items    = new item[capacity];
    backPtr  = new item;
    frontPtr = new item;
    midPtr   = new item;
    current  = new item;

    maxSize = capacity;
    back    = maxSize-1;
    count   =  0;
    top     = -1;
}

quack::~quack(void)
{
    delete frontPtr;
    delete backPtr;
    delete current;
    delete midPtr;
    delete [] items; //Heap Corruption Debug Error at the end of program.

    items    = NULL;
    maxSize  =  0;
    back     =  0;
}

bool quack::pushFront(const int …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
908
查看次数

C++ HashTable对象实现

int hazmat::hashStr(char const * const str)
{  
    int count = 0;
    for ( unsigned i = 0; i < strlen( str ); i++ )
    {
        count += str[i]; // get the ascii sum.
    }
    return count % maxSize;  
}
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
2
解决办法
867
查看次数

获取适当的哈希索引C++

我已经尝试了一切.即使是java的论坛:

java.lang.String.hashCode():

s[0]*(31^(n-1)) + s[1]*(31^(n-2)) + ... + s[n-1]
Run Code Online (Sandbox Code Playgroud)

我把它解释为一个总和:虽然我不太清楚如何处理s [n-1];

int hashmap::hashstr( const char*const str )
{
    int result = 0;
    int i = 0;
    int j = 0;
    int k = 0;
    unsigned n = 0;
    for ( ; str[n] != NULL; ++n ); // how many characters?

    while ( j != n ) // no more characters
    {
        result += str[i] * ( 31 ^ ( n - k ) );
        j++;
        i++;
        k++;
    }
    return result …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
430
查看次数

来自第三类对象C++的垃圾值

以下是股票来源的实施文件的片段:

 stock::stock(char const * const symbol, char const * const name, int sharePrice, date priceDate )
 : m_sharePrice( sharePrice )
 {
    if ( symbol )
    {
        size_t length = strlen( symbol ) +1;
        m_symbol = new char[length];
        strcpy_s( m_symbol, length, symbol );
    }
    else m_symbol = NULL;
    if ( name )
    {
            size_t length = strlen( name ) +1;
       m_name = new char[length];
       strcpy_s( m_name, length, name );
    }
    else m_name = NULL;

    dateObj = &priceDate;
 }
Run Code Online (Sandbox Code Playgroud)

这就是我用cstrings分配内存的方法.在主要的情况下,参数传递的方式类似于"符号",名称",10,date :: JANUARY,19,1677.日期是一个对象,其中的月份是enumaterted.这是最后一个参数"日期"类型是什么iim当我从一个源文件移动到下一个源文件时,我无法保留属性.我看到在上面的函数中,"dateObj"具有我需要的属性.但是当我将它转移到另一个源文件时价值观已经消失...... …

c++

0
推荐指数
1
解决办法
374
查看次数

C++运算符重载时的垃圾值

我只是得到垃圾值.并且调试器显示正确的值是很奇怪的.但它印刷的怪异东西......

这个第一部分很好.从本质上讲,它只是把我带到我的问题.我有我需要在h.hashtable [hashIndex]数组中打印的内容.

ostream& operator<<(ostream& out, const hashmap& h)
{
        const char *getSymbol = NULL;
        for ( int hashIndex = 0; hashIndex < maxSize; hashIndex++ )
        {   
            getSymbol = h.hashTable[hashIndex].getSymbol();
            if ( getSymbol ) // Find the one I added.
            {
                h.hashTable->display(out);
                    return out << h.hashTable[hashIndex];
            }
        }
        return out;
}
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
375
查看次数

从HashTable C++中删除

你如何从基于数组的哈希表中删除?我需要准备从表中删除几个符号.如果我在固定大小的字符数组中转储我要删除的内容,那么如何找到"可能"删除的内容?

bool hashmap::get(char const * const symbol, stock& s) const
{
    int hashVal = this->hashStr( symbol );
    int initialHash = -1;

    while ( hashTable[hashVal].m_symbol != NULL )
    {   // try to find a match for the stock associated with the symbol.

        if ( initialHash == -1 )
        {
             initialHash = hashVal;
        }
        if ( strcmp( hashTable[hashVal].m_symbol, symbol ) == 0 )
        {
            s = &hashTable[hashVal];
            return true;
        }
        ++hashVal %= maxSize;
    }
    if ( hashTable[hashVal].m_symbol == NULL || hashVal == …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
2692
查看次数

数组重新分配C++

假设你有一个容量为5的数组,并且假设你有一个计数变量,它计算添加到数组中的每个条目.你怎么会真正的数组呢?使用C++语法?

void BST::reallocate()
{
    item *new_array = new item[size*2]; 
 for ( int array_index = 0; array_index < size * 2; array_index++ ) 
    {
        if ( ! items[array_index].empty )
        {
   new_array[array_index].theData = items[array_index].theData;
   new_array[array_index].empty = false;
  }
    }
    maxSize += size;
    delete [] items;

    items = NULL;
    items = new_array;
 }
Run Code Online (Sandbox Code Playgroud)

你如何重新分配数组?BST ctor下面是私有项结构,只是为了消除任何混淆.

BST::BST(int capacity) : items(new item[capacity]), Position(0), 
leftChild(0), rightChild(0), maxSize(capacity)
{

}
Run Code Online (Sandbox Code Playgroud)

这是在BST标题中:

private:
 int size;
 int maxSize;
 int Position;
 int leftChild;
 int rightChild;
 struct item
 {
  bool empty; …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
2
解决办法
1386
查看次数

标签 统计

c++ ×10

linked-list ×1