1
我创建了一个数据包结构。我读了一个文件的文本并转换为字典顺序。为了做到这一点,我必须将两个字符串转换为小写字母来比较它们(一个用于当前节点,另一个用于它旁边的节点)。但是我的问题是当我有大的文本文件时,它必须不断将字符串转换为小写字母,以便插入每个节点,并且有时需要很长时间才能处理。我想知道是否有更好的方法来调整这个方法,这样我就可以提高性能。调试时间问题
void insert(string v)
{
if(head == NULL){ //empty list
head = new BagNode;
head->dataValue = v;
//head->dataCount = 0;
head->next = NULL;
}
else
{
BagNode * n = new BagNode; // new node
n->dataValue = v;
BagNode * current = head; //for traversal
//current = head;
n->dataCount = 0;
if(!isBefore(current->dataValue, v)) //new head
{
n->next = head;
head = n;
}
else{ //mid and tail insert
while(current->next && isBefore(current->next->dataValue,v))
{
current = current->next;
}
n->next = current->next;
current->next = n;
}
}
}
比较两个节点
bool isBefore(string a, string b)
{
transform(a.begin(), a.end(), a.begin(), ::tolower);
transform(b.begin(), b.end(), b.begin(), ::tolower);
if(a == b) {
return true;
}
else {
return false;
}
}
在节点中保留原始字符串和转换后的字符串。第一个用于应用程序,第二个用于比较。为插入做同样的事情:转换一次,寻找正确的位置,并插入原始和转换。像往常一样,你会交易空间的时间。 – GMichael
'NULL'用于C99。在C++ 11中使用'nullptr'。顺便说一句,你应该使用C++ [标准容器](http://en.cppreference.com/w/cpp/container)&[智能指针](http://en.cppreference.com/w/cpp/memory) –
也使用所有警告和调试信息编译(如果使用[GCC](http://gcc.gnu.org/),使用'g ++ -Wall -g')并使用调试器('gdb')&[valgrind] (http://valgrind.org/) –