创建字符串向量:C++

Creating a vector of strings : C++

本文关键字:C++ 向量 字符串 创建      更新时间:2023-10-16

我正在尝试创建一个向量,该向量应输入strings (white space included),直到用户输入'!'

以下是我的代码:

#include <iostream>
#include <vector>
#include <string>
#include <iterator>
using namespace std;

int main()
{
   char buffer[100];
   string temp;
   vector <string> vec;

    while(1)//Shows Segmentation fault error while running,
    //compiles succesfully
    {
    cout << "Enter String : ";
    cin.get(buffer,100,'n');//To ensure that whitespaces are also input
    cout << "@@@@ " << buffer << endl ;//Shows correct input
    cin.ignore(std::numeric_limits<std::streamsize>::max(),'n');
    //Clearing cin stream for any residual characters
    temp.assign(buffer);//Assigning string the input "phrase"
    cout << "###" << temp << endl;//Shows correct output
    if (temp == "!") //if input is '!' character do not push on the vector a
    //come out of the loop
    break;
    vec.push_back(temp);//push on the vector
    temp.assign(NULL); //flush temp string
    }

  vector <string>::iterator p = vec.begin();
  //display thre vector
  while( p != vec.end())
   {
       cout << *p << endl;
       p++;
       }
    return 0;
    }

它成功编译,但在运行时抛出Segmentation fault错误。

不知道为什么?有人能指出吗?

此外,对于这一更智能的解决方案,我们很感激同时指出我的代码有什么问题。

感谢

这将是原因:

temp.assign(NULL);

因为std::string::assign()将尝试读取,直到找到空终止符,并且取消引用NULL指针是未定义的行为:在这种情况下是分段错误。使用temp.clear(),或者在每次迭代中只创建一个新对象。

使用std::getline(),它读取包括空白的行,并避免对固定大小的数组(即buffer[100](进行硬编码:

std::string line;
while (std::getline(std::cin, line) && line != "!")
{
    vec.push_back(line);
}

尽管存在其他问题,但在gdb(或任何其他调试器(中运行程序可以揭示原因:

tikal@seven ~$ g++ -o temp temp.cpp
tikal@seven ~$ gdb temp
GNU gdb 6.3.50-20050815 (Apple version gdb-1822) (Sun Aug  5 03:00:42 UTC 2012)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries ... done
(gdb) run
Starting program: /Users/tikal/temp 
Reading symbols for shared libraries ++............................. done
Enter String : asd
@@@@ asd
###asd
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x00007fff9081e6b0 in strlen ()
(gdb) backtrace
#0  0x00007fff9081e6b0 in strlen ()
#1  0x00007fff9857ab95 in std::string::assign ()
#2  0x0000000100001642 in main ()
(gdb) 

从本质上讲,temp.assign( NULL )是个坏主意。您可以使用temp.clear(),或者不必费力地清除它(稍后只需重新分配(。

while (getline(cin, temp) && temp != "!")
{
    vec.push_back(temp);
}

使用这样的缓冲区比C++更像是一件C的事情。在C++中,通常有一种方法可以避免使用类进行这种显式内存管理——这是一个很好的例子。