C++关于类模板(vector)的问题。

今天写了一段代码要求实现:
从标准输入读取字符串并将他们存储为类模板(vector)对象,并从类模板中将其输出。
程序编译后提示有4个警告如下:
warningC4786: '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_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' chara
cters in the debug information
我是初学者,这个警告好像是内存出了问题,请大家帮我大概解释一下这里的提示是什么意思。
还有一个问题,我可能是少引用了一个std命名空间中的操作,所以在编译过程中总是出错,但是加上using namespace std;后就能编译通过了。请大家帮我看一下是缺了哪个std中的操作。
代码如下:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
vector<string> text;
string word;
while(cin>>word)
{
text.push_back(word);
}
for(vector<int>::size_type index=0;index!=text.size();index++)
cout<<text[index]<<" ";
cout<<endl;
return 0;
}
还有我刚开始学习C++,代码中有什么其他的不足希望大家多多斧正。

main函数的应用程序的入口函数,不能定义成模板函数。
模板函数模板类都是在调用(或者实例化)的时候,被指定了参数类型之后才生成真正的代码的。可以查相关的书籍《thinking in C++》《stl 源码剖析》等。
你改了main函数的原型,当然会出现链接错误。

vector本身是模板类,你可以指定任意的类型装进去。
作为模板类,vector能够保存“任意类型”的数据,是指相对于vector这个类来说,他不关心保存的是什么类型的。但是当你实例化一个vector的时候,你就需要确定一个参数类型,这样你才能拥有一个可以用来实例化一个具体对象的类类型,比如vector<int>,vector<string>,vecor<Dog>等。

任意类型是相对于模板类的,不是相对于应用程序。对于一个应用程序,他的逻辑是确定的,他能处理的数据类型也是确定的。即使是某些可以产生随意性结果的程序,他的逻辑在一定程度上也是确定的。

我不知道我说没说清楚,如果还有问题,请提出。
追问

main函数的应用程序的入口函数指的是while(cin>>word){text.push_back(word);}吗?如果是的话说他不能定义成模板函数,这是为什么呢?这样做程序可以正常运行,会带来哪些潜在的威胁呢?还有您推荐的以查相关的书籍《thinking in C++》《stl 源码剖析》在网上可以找到电子版吗?或者哪些是值得推荐仔细研读的。不然为了一个问题去买两本书个人觉得有点事倍功半的意思。望指教。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-13
可以去掉using namespace std,因为程序中你用到的std名字空间的类型都用了using指示声明,这是可以做到的。至于你说编写头文件时,用std::使用std名字空间中的类型,这是为了防止std在头文件中滥用而造成“名字污染”,这刚好和引进名字空间的目的相反,事与愿违了!
第2个回答  2011-09-08
warningC4786: '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_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' chara
cters in the debug information
在调试信息里,标识符'std::reverse_ite........................cator<char> > *,int>'被截断到255个字符
_________
我这不用using namespace std;没事啊, 除了vc6,
你多加个using std::vector<int>; 试试
不过text是vector<string>的啊, 你用vector<int>::size_type干嘛?
用vector<string>::size_type; 和using std::vector<string>;

或者你换个高版本的或其他的编译器试试
第3个回答  推荐于2016-03-27
你的代码没什么大问题,只是有两个地方需要修改一下,如下(见注释):
=====================================================
#include<iostream>
#include<string>
#include<vector>
using namespace std;//只需要使用此句就可以了
//using std::cin;
//using std::cout;
//using std::endl;
//using std::string;
//using std::vector;
int main()
{
vector<string> text;
string word;
//while(cin>>word;)//cin>>word执行后返回的类型为istream类型,一般不能通过判断它的执行结果来控制循环
//应该在循环内部判断输入的值来控制是否退出循环
while(true)
{
cin>>word;
text.push_back(word);
//如果输入"exit"则退出
if ( word.compare("exit")==0)
{
break;
}
}
for(vector<int>::size_type index=0;index!=text.size();index++)
cout<<text[index]<<" ";
cout<<endl;

return 0;
}追问

我知道使用using namespace std;是包含下面的操作,只是在编写头文件的时候,所有的引用于std的操作需要通过using std::XXX的方式声明。我想知道如果去掉using namespace std;而全部改为using std::XXX声明,我的代码中缺少什么。

追答

我试过了,目前你代码里面不用using namespace std;采用你的方式也能编译和执行,不缺少什么.

本回答被提问者采纳
第4个回答  推荐于2018-03-28
#include <iostream>
#include <algorithm>
#include <iterator>
int main(int argc, char* argv[])
{
std::copy(&argv[1],&argv[argc],
std::ostream_iterator<char*>(std::cout,"\t"));

return argc != 1;
}
比你写的简单多了。本回答被网友采纳
相似回答