创建字符串向量:C++
Creating a vector of strings : C++
我正在尝试创建一个向量,该向量应输入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++中,通常有一种方法可以避免使用类进行这种显式内存管理——这是一个很好的例子。
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么