首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

str.push_back为啥这段程序在dev下能通过,在vc6.0下不能

2013-09-12 
str.push_back为什么这段程序在dev下能通过,在vc6.0下不能?#includeiostream#includevector//#include

str.push_back为什么这段程序在dev下能通过,在vc6.0下不能?
#include<iostream>
#include<vector>
//#include<stdlib.h>
#include<string>
using namespace std;
int main()
{
    string str("hello");
    for(int i=0;str[i]!='\0';i++)
    {
        cout<<str[i];
    }
    cout<<endl;
    cout<<"the length of str = "<<str.size()<<endl;
    str.push_back('h');
    cout<<"the length of str = "<<str.size()<<endl;
    str.push_back('\0');
    cout<<"the length of str = "<<str.size()<<endl;
    cout<<str<<endl;
    //system("PAUSE");
    return 0;
}        
    
error C2039: 'push_back' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
[解决办法]
#include<iostream.h>
...
去掉:
using namespace std;

[解决办法]
VC6对标准支持不好,所有STL的头文件,都以.h结尾的。
而且,没有std命名空间。
[解决办法]

引用:
#include<iostream>
#include<vector>
//#include<stdlib.h>
#include<string>
using namespace std;
int main()
{
    string str("hello");
    for(int i=0;str[i]!='\0';i++)
    {
        cout<<str……



兄弟,  str.push_back('\0');  str是string类型的,没有push_back这个操作函数,插入一个字符,你应该用insert()函数啊。
------解决方案--------------------


#include<iostream>
#include<vector>
//#include<stdlib.h>
#include<string>
using namespace std;
int main()
{
  string str("hello");
  for(int i=0;str[i]!='\0';i++)
  {
  cout<<str[i];
  }
  cout<<endl;
  cout<<"the length of str = "<<str.size()<<endl;
  str.insert('h');   //str.push_back('h');
  cout<<"the length of str = "<<str.size()<<endl;
   str.insert('\0');  //str.push_back('\0');
  cout<<"the length of str = "<<str.size()<<endl;
  cout<<str<<endl;
  //system("PAUSE");
  return 0;
}  


编译通过。
    

热点排行