64,632
社区成员
发帖
与我相关
我的任务
分享
//test_main.h
#include <iostream>
#include "test.h"
using namespace std;
int main()
{
cout << count("abcda", 'a') << endl;
return 0;
}
//test.h
#ifndef COUNT_H
#define COUNT_H
int count(const string &str, char ch);
#endif
//test_count.cpp
#include "test.h"
#include "string"
int count(const string &str, char ch)
{
int count = 0;
for(string::size_type ix = 0; ix != str.size(); ++ix)
{
if(str[ix] == ch)
++count;
}
return count;
}
//test_main.cpp
#include <iostream>
#include "test.h"
using namespace std;
int main()
{
std::string str="abcda";
cout << count(str, 'a')<< endl;
return 0;
}
#ifndef COUNT_H
#define COUNT_H
#include <string>
extern int count(const std::string &, char );
#endif
//test_count.cpp
#include <string>
class string ;
int count(const std::string &str, char ch)
{
int count = 0;
for(std::string::size_type ix = 0; ix != str.size(); ++ix)
{
if(str[ix] == ch)
++count;
}
return count;
}
std::string str;
// test_main.cpp
#include <iostream>
#include "string"
#include "test.h"
using namespace std; // 命名空间声明
int main()
{
cout << count("abcda", 'a') << endl; //的确,const引用 是可以接受 字符串字面值
return 0;
}
// test.h
#ifndef COUNT_H
#define COUNT_H
#include <string>
int count(const std::string &str, char ch);
#endif
//test_count.cpp
#include "test.h" // 这个添加可以让编译器帮助检查 函数声明是否正确,特意加的。
#include "string"
using namespace std; // 可以像 soloopin兄弟那样,std::string,也可以直接声明。
// 不过,我不知道哪个好,哪个坏。
// 感觉上,还是 soloopin 那样做好。原因:凭感觉。
int count(const string &str, char ch)
{
int count = 0;
for(string::size_type ix = 0; ix != str.size(); ++ix)
{
if(str[ix] == ch)
++count;
}
return count;
}