#include <string>
#include <vector>
#include "math.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::getline;
using std::vector;
int main()
{
string word;
vector<string> s;
while(cin >> word)
s.push_back(word);
size_t n=s.size();
string *p=new string[n];
for(std::vector<string>::size_type ix=0;ix!=s.size();++ix)
*(p+ix)=s[ix];
for(string *q=p;q!=q+n;++q)
cout << *q << endl;
return 0;
}
这个程序就是要读入一组string数据,并存到vector中,接着把这个vector对象复制到一个字符指针数组,然后输出。
编译通过但运行的时候windows会弹出错误,但运行结果正常。
这是怎么回事,是不是哪写的不合适。
11 个解决方案
#1
你说下面那些吗?你用的是VC吧?VC就这样子,不知道什么毛病
:\mfcwork\csdn\csdn.cpp(1241) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char
_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
E:\mfcwork\csdn\csdn.cpp(1241) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_trait
s<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
csdn.obj - 0 error(s), 4 warning(s)
:\mfcwork\csdn\csdn.cpp(1241) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char
_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
E:\mfcwork\csdn\csdn.cpp(1241) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_trait
s<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
csdn.obj - 0 error(s), 4 warning(s)
#2
你是在DEBUG状态下编译吧?
#3
如果是debug,把STL扩展开来时,程序中所出现的标识符长度限制在了255个字符范围内
就会出现你的错误(这些标识符就无法在调试阶段查看和计算了)
可以这样在程序头加#pragma warning(disable: 4786)
#4
如果是debug,把STL扩展开来时,程序中所出现的标识符长度限制在了255个字符范围内
就会出现你的错误(这些标识符就无法在调试阶段查看和计算了)
可以这样在程序头加#pragma warning(disable: 4786)
#5
前面帖子已说过,编译器问题(可能版本低),
我以前用intel C++的编译器就没有此问题了。
我以前用intel C++的编译器就没有此问题了。
#6
哦楼主我知道了,我把你的程序改好了,我已经运行正常,改的地方在下面:
#include <iostream>
#include <string>
#include <vector>
#include "math.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::getline;
using std::vector;
int main()
{
string word;
vector<string> s;
while(cin >> word)
s.push_back(word);
size_t n=s.size();
string *p=new string[n];
for(std::vector<string>::size_type ix=0;ix!=s.size();++ix)
*(p+ix)=s[ix];
for(string *q=p;q!=p+n;++q) //这里原来写是结束条件是q!=q+n,要改成q!=p+n
cout << *q << endl; //因为q一直在自加,q!=q+n是个死循环,所以导致程序进入了
return 0; //非法内存区去读数值,出现内存错误
}
#include <iostream>
#include <string>
#include <vector>
#include "math.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::getline;
using std::vector;
int main()
{
string word;
vector<string> s;
while(cin >> word)
s.push_back(word);
size_t n=s.size();
string *p=new string[n];
for(std::vector<string>::size_type ix=0;ix!=s.size();++ix)
*(p+ix)=s[ix];
for(string *q=p;q!=p+n;++q) //这里原来写是结束条件是q!=q+n,要改成q!=p+n
cout << *q << endl; //因为q一直在自加,q!=q+n是个死循环,所以导致程序进入了
return 0; //非法内存区去读数值,出现内存错误
}
#7
我猜是楼主笔误了吧
#8
才看到楼主只学了一个月的C++,那我再说说吧.
不知道楼主是否看懂了死循环所在,如果按楼主原来写的:
for(string *q=p;q!=q+n;++q)
执行一次,q加了1,现在q是q+1了
而条件是q!=q+n,我们假设n是3好了(反正它也是一个常数),
那么q=q+3吗? 显然不等.
于是再循环,q再加1,变成q+2,
q=q+2了,q+2=q+2+3吗?仍然不等.
死循环了.
其实从数学角度就看出来了
q=q+n的条件那就是n=0,然而你的程序里n当然是不等于0的,所以程序会无限的循环下去.
这样就导致了你的q走到了没有开辟的内存那里,而产生非法的访问内存错误.
改成q!=p+n之后就好了,因为只有q是变的,那两个都不变,当然不是死循环,
不知道我说懂了否
不知道楼主是否看懂了死循环所在,如果按楼主原来写的:
for(string *q=p;q!=q+n;++q)
执行一次,q加了1,现在q是q+1了
而条件是q!=q+n,我们假设n是3好了(反正它也是一个常数),
那么q=q+3吗? 显然不等.
于是再循环,q再加1,变成q+2,
q=q+2了,q+2=q+2+3吗?仍然不等.
死循环了.
其实从数学角度就看出来了
q=q+n的条件那就是n=0,然而你的程序里n当然是不等于0的,所以程序会无限的循环下去.
这样就导致了你的q走到了没有开辟的内存那里,而产生非法的访问内存错误.
改成q!=p+n之后就好了,因为只有q是变的,那两个都不变,当然不是死循环,
不知道我说懂了否
#9
笔误害人。。
#10
看来打题的人还是很细心的嘛
#11
#12
知道了 确实笔误了 没注意看 谢谢大家了 分数给第一位了
#1
你说下面那些吗?你用的是VC吧?VC就这样子,不知道什么毛病
:\mfcwork\csdn\csdn.cpp(1241) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char
_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
E:\mfcwork\csdn\csdn.cpp(1241) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_trait
s<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
csdn.obj - 0 error(s), 4 warning(s)
:\mfcwork\csdn\csdn.cpp(1241) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char
_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
E:\mfcwork\csdn\csdn.cpp(1241) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_trait
s<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
csdn.obj - 0 error(s), 4 warning(s)
#2
你是在DEBUG状态下编译吧?
#3
如果是debug,把STL扩展开来时,程序中所出现的标识符长度限制在了255个字符范围内
就会出现你的错误(这些标识符就无法在调试阶段查看和计算了)
可以这样在程序头加#pragma warning(disable: 4786)
#4
如果是debug,把STL扩展开来时,程序中所出现的标识符长度限制在了255个字符范围内
就会出现你的错误(这些标识符就无法在调试阶段查看和计算了)
可以这样在程序头加#pragma warning(disable: 4786)
#5
前面帖子已说过,编译器问题(可能版本低),
我以前用intel C++的编译器就没有此问题了。
我以前用intel C++的编译器就没有此问题了。
#6
哦楼主我知道了,我把你的程序改好了,我已经运行正常,改的地方在下面:
#include <iostream>
#include <string>
#include <vector>
#include "math.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::getline;
using std::vector;
int main()
{
string word;
vector<string> s;
while(cin >> word)
s.push_back(word);
size_t n=s.size();
string *p=new string[n];
for(std::vector<string>::size_type ix=0;ix!=s.size();++ix)
*(p+ix)=s[ix];
for(string *q=p;q!=p+n;++q) //这里原来写是结束条件是q!=q+n,要改成q!=p+n
cout << *q << endl; //因为q一直在自加,q!=q+n是个死循环,所以导致程序进入了
return 0; //非法内存区去读数值,出现内存错误
}
#include <iostream>
#include <string>
#include <vector>
#include "math.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::getline;
using std::vector;
int main()
{
string word;
vector<string> s;
while(cin >> word)
s.push_back(word);
size_t n=s.size();
string *p=new string[n];
for(std::vector<string>::size_type ix=0;ix!=s.size();++ix)
*(p+ix)=s[ix];
for(string *q=p;q!=p+n;++q) //这里原来写是结束条件是q!=q+n,要改成q!=p+n
cout << *q << endl; //因为q一直在自加,q!=q+n是个死循环,所以导致程序进入了
return 0; //非法内存区去读数值,出现内存错误
}
#7
我猜是楼主笔误了吧
#8
才看到楼主只学了一个月的C++,那我再说说吧.
不知道楼主是否看懂了死循环所在,如果按楼主原来写的:
for(string *q=p;q!=q+n;++q)
执行一次,q加了1,现在q是q+1了
而条件是q!=q+n,我们假设n是3好了(反正它也是一个常数),
那么q=q+3吗? 显然不等.
于是再循环,q再加1,变成q+2,
q=q+2了,q+2=q+2+3吗?仍然不等.
死循环了.
其实从数学角度就看出来了
q=q+n的条件那就是n=0,然而你的程序里n当然是不等于0的,所以程序会无限的循环下去.
这样就导致了你的q走到了没有开辟的内存那里,而产生非法的访问内存错误.
改成q!=p+n之后就好了,因为只有q是变的,那两个都不变,当然不是死循环,
不知道我说懂了否
不知道楼主是否看懂了死循环所在,如果按楼主原来写的:
for(string *q=p;q!=q+n;++q)
执行一次,q加了1,现在q是q+1了
而条件是q!=q+n,我们假设n是3好了(反正它也是一个常数),
那么q=q+3吗? 显然不等.
于是再循环,q再加1,变成q+2,
q=q+2了,q+2=q+2+3吗?仍然不等.
死循环了.
其实从数学角度就看出来了
q=q+n的条件那就是n=0,然而你的程序里n当然是不等于0的,所以程序会无限的循环下去.
这样就导致了你的q走到了没有开辟的内存那里,而产生非法的访问内存错误.
改成q!=p+n之后就好了,因为只有q是变的,那两个都不变,当然不是死循环,
不知道我说懂了否
#9
笔误害人。。
#10
看来打题的人还是很细心的嘛
#11
#12
知道了 确实笔误了 没注意看 谢谢大家了 分数给第一位了