的std ::矢量<字符串* GT;崩溃...... [英] std::vector<string*> crash...

查看:58
本文介绍了的std ::矢量<字符串* GT;崩溃......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我是STL的新手,我写了以下代码

哪个崩溃了。

有谁能告诉我为什么这个代码会崩溃?

感谢提前。

-

JF

#include< iostream>

#include< vector>


using namespace std;


static char data [] =

"因为组成Linux的原始数量和指令已经发布了,\

任何程序员都可以看到它在做什么,\\ /

它是如何做到的,可能还有它如何做到这一点。 Torvalds没有发明开放式编程的概念,但Linu \

x是它的第一个成功故事。事实上,它可能无法取得成功,因为互联网已经将不同的计算专家联系在一起。\\ b
世界的计算专家。在制作\

Linux是一种开放的语言时,托瓦兹给了他一个富裕的机会,让他们的工作变得富裕起来。这也是书呆子的一部分,这取决于作者身份的满意度和对b的尊重。 $ b ne?s同行。;


static const int line_length = 10;


int

main(int argc,char ** argv)

{

string * str = NULL;

vector< string *> v;

vector< string *> :: iterator it;


for(int i = 0; i< sizeof(data)/ sizeof( data [0]); i ++)

{

if(0 ==((i + 1)%line_length))

{

//每个line_length char创建一个新的

//字符串,它将保留该行的位置。


str = new string;

str- > insert(str-> end(),'a'');

v.insert(v.end(),str);

}


it = v.end();

// CRASH HERE。

(* it) - > insert(( * it) - > end(),data [i]);

}


for(it = v.begin(); it!= v.end();它++)

{

cout<< **它<< endl;

删除(* it);

}

}

Hello,
I am new to STL and I''ve written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.
--
J-F
#include <iostream>
#include <vector>

using namespace std;

static char data[] =
"Because the original quantities and instructions \
that make up Linux have been published, \
any programmer can see what it is doing,\
how it does it and, possibly, how it cou\
ld do it better. Torvalds did not invent\
the concept of open programming but Linu\
x is its first success story. Indeed, it\
probably could not have succeeded befor\
e the Internet had linked the disparate\
world of computing experts. In making \
Linux an open language, Torvalds gave \
up the opportunity of growing rich fro\
m his work. This too is part of nerd c\
ulture, which thrives on the satisfact\
ion of authorship and the respect of o\
ne?s peers.";

static const int line_length = 10;

int
main(int argc, char** argv)
{
string* str = NULL;
vector<string*> v;
vector<string*>::iterator it;

for( int i = 0; i < sizeof(data) / sizeof(data[0]); i++)
{
if( 0 == ((i+1) % line_length))
{
// Every "line_length" char create a new
// string which will hold place for the line.

str = new string;
str->insert(str->end(), ''a'');
v.insert(v.end(), str);
}

it = v.end();
// CRASH HERE.
(*it)->insert((*it)->end(), data[i]);
}

for(it = v.begin(); it != v.end(); it++)
{
cout << **it << endl;
delete(*it);
}
}

推荐答案



" dalbosco" <哒****** @ free.fr>在消息中写道

news:41 *********************** @ news.free.fr ...

"dalbosco" <da******@free.fr> wrote in message
news:41***********************@news.free.fr...
您好,
我是STL的新手,我写了以下代码
哪个崩溃了。
有谁能告诉我为什么这个代码会崩溃?
int
main(int argc,char ** argv)
{
string * str = NULL;
vector< string *> v;
vector< string *> :: iterator it;

for(int i = 0; i< sizeof(data)/ sizeof(data [0]); i ++)
{
if(0 ==((i + 1)%line_length))
//
line_length" char创建一个新的
//字符串,它将为该行保留位置。

str = new string;
str-> insert(str-> end(), ''a'');
v.insert(v.end(),str);
}

it = v.end();


现在''它'指向向量之外。它没有

指的是一个有效的对象。

// CRASH HERE。
(* it) - > insert((* it) - >结束(),数据[i]);
Hello,
I am new to STL and I''ve written the following code
which crash.
Could anyone tell me why this code crash? int
main(int argc, char** argv)
{
string* str = NULL;
vector<string*> v;
vector<string*>::iterator it;

for( int i = 0; i < sizeof(data) / sizeof(data[0]); i++)
{
if( 0 == ((i+1) % line_length))
{
// Every "line_length" char create a new
// string which will hold place for the line.

str = new string;
str->insert(str->end(), ''a'');
v.insert(v.end(), str);
}

it = v.end();
Now ''it'' is pointing outside the vector. It does not
refer to a valid object.
// CRASH HERE.
(*it)->insert((*it)->end(), data[i]);




在这里你取消引用''它''。未定义的行为。


-Mike



Here you dereference ''it''. Undefined behavior.

-Mike


dalbosco写道:
dalbosco wrote:
你好,
我我是STL的新手,我写了下面的代码
哪个崩溃。
有谁能告诉我为什么这个代码崩溃了?
感谢提前。
-
JF

#include< iostream>
#include< vector>
使用命名空间std;

静态字符数据[ ] =
" [...]" ;;

static const int line_length = 10;

int
main(int argc,char ** argv)
{
string * str = NULL;
vector< string *> v;


为什么世界上你需要_pointers_的向量?为什么不只是

a矢量字符串?

vector< string *> :: iterator it;

for(int i = 0; i < sizeof(data)/ sizeof(data [0]); i ++)
{
if(0 ==((i + 1)%line_length))
{
//每个line_length char创建一个新的
//字符串,它将为该行保留位置。
str = new string;
str-> insert(str-> end(),''a' );


为什么你需要这个而不是,比如说,


str-> append(''a'');




v.insert(v.end(),str);


写得好多了


v.push_back(str);


it'这更容易理解。

}

它= v.end();
// CRASH在这里。
(* it) - > insert((* it) - > end(),data [i]);


你想在这里实现什么?如果你需要

_last_元素的迭代器,也许你想用

''v.rbegin()''而不是''v.end()''矢量。 ''v.end()''返回one-past-the-

end迭代器。


RTFM

}

for(it = v.begin(); it!= v.end() ;它++)
{
cout<< **它<< endl;
删除(* it);
}
}
Hello,
I am new to STL and I''ve written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.
--
J-F
#include <iostream>
#include <vector>

using namespace std;

static char data[] =
"[...]";

static const int line_length = 10;

int
main(int argc, char** argv)
{
string* str = NULL;
vector<string*> v;
Why in the world do you need a vector of _pointers_? Why not just
a vector of strings?
vector<string*>::iterator it;

for( int i = 0; i < sizeof(data) / sizeof(data[0]); i++)
{
if( 0 == ((i+1) % line_length))
{
// Every "line_length" char create a new
// string which will hold place for the line.
str = new string;
str->insert(str->end(), ''a'');
Why do you need this instead of, say,

str->append(''a'');

?
v.insert(v.end(), str);
This is much better written

v.push_back(str);

it''s much easier to understand that.
}

it = v.end();
// CRASH HERE.
(*it)->insert((*it)->end(), data[i]);
What are you trying to achieve here? Perhaps you wanted to use
''v.rbegin()'' instead of ''v.end()'' if you need the iterator to the
_last_ element of the vector. ''v.end()'' returns the "one-past-the-
end" iterator.

RTFM
}

for(it = v.begin(); it != v.end(); it++)
{
cout << **it << endl;
delete(*it);
}
}



2004年10月22日星期五19:39 :04 +0200,dalbosco< da ****** @ free.fr>写道:
On Fri, 22 Oct 2004 19:39:04 +0200, dalbosco <da******@free.fr> wrote:
你好,
我是STL的新手,我写了下面的代码
哪个崩溃了。
谁能告诉我为什么这个代码崩溃了吗?
感谢提前。
Hello,
I am new to STL and I''ve written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.




在编写实际代码时你不能使用行继续(反斜杠),

仅在编写宏时(#define ...)


请尝试这样做:


静态字符数据[] =

"因为构成Linux的原始数量和说明

已经发布,

任何程序员都可以看到它正在做,





编译器只是自动连接这些行。如果你想要

实际换行符,你需要在数据中嵌入''\ n''。


-

Bob Hairgrove
没有********** @ Home.com


这篇关于的std ::矢量&lt;字符串* GT;崩溃......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆