|
1. STL 中 string 转换大小写的问题
程序如下:
#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;
int main()
{
// create a string
string s("This is a string");
cout << "original: " << s << endl;
// lowercase all characters
transform(s.begin(), s.end(), s.begin(), tolower);
cout << "lowered: " << s << endl;
// uppercase all characters
transform(s.begin(), s.end(), s.begin(), toupper);
cout << "uppered: " << s << endl;
}
编译的时候报了一大堆错,如下:
test.cpp: In function `int main()':
test.cpp:13: no matching function for call to `transform(
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
test.cpp:19: no matching function for call to `transform(
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
请问如何解决?我用的是 RH9 自带的 3.2.2
2. C 中 strcpy 的问题:
程序如下:
#include <string.h>
#include <stdio.h>
int main()
{
char * a = "This is a string";
char * b;
printf("%s\n",a);
b = strcpy(b, a);
printf("%s\n",b);
}
如上编译运行没有任何错误,并且正常打印出 a 和 b ,但是如果把第一个 printf 语句,就是打印 a 的那个语句删除,或者将 strcpy 挪到这个 printf 之前,编译没有问题,运行的时候就会报告“段错误”。
请大家帮助我看看是什么问题,刚开始学习 C++ 和 C,谢谢! |
|