要求的代码审查,Accelerated C ++ [英] Code review requested, Accelerated C++

查看:62
本文介绍了要求的代码审查,Accelerated C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对Accelerated C ++问题5-10的回答。我很感激

你的伙伴们的任何评论(关于风格,效率,标准等)

有。


结果程序看起来相当快,在我的PowerBook G4上用3秒内的300,000+单词搜索

字典。


//编写一个找到所有程序的程序字典中的回文,

//也找到最大的回文。


#include< iostream>

#使用std :: cout包含< vector>


;使用std :: cin;

使用std :: string;使用std :: endl;

使用std :: vector;


bool compare_word_length (const string&,const string&);


int main()

{

cout<< 该程序将在字典中找到所有的回文>

"并且将找到最大的回文。 << std :: endl;


字符串输入;

vector< string>回文;


//得到回文

而(cin>>输入)

if(is_palindrome(输入) )

palindromes.push_back(输入);


//显示回文

cout<< \ n \\ n \\ n \\ n \\ n \\ n \\ n \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ = palindromes.end(); ++ iter)

{

cout<< * iter<< ''\t'';

}

cout<< ''\ n'';


//排序回文

sort(palindromes.begin(),palindromes.end(),compare_word_length);


//显示最短和最长的一个

string smallest = * palindromes.begin();

string largest = *( palindromes.end() - 1);


cout<< 最短的回文是'' <<最小的<< "''"

"而最长的一个是'' <<最大的<< "''\ n";


cout<< endl;


返回0;

}


bool is_palindrome(const string& input)

{

string :: const_iterator begin,end;

begin = input.begin();

end = input。结束() - 1;


while(begin< end)

{

if(* begin!= * end )

返回false;

开始++;

结束 - ;

}

返回true;

}


bool compare_word_length(const string& a,const string& b)

{

返回(a.size()< b.size());

}

解决方案

2004-10-16 17:26:45 -0700,Joe Van Dyk< jo ******* @ nospam.gmail.com>说:

这是我对Accelerated C ++问题5-10的回答。我很感激
任何评论(关于风格,效率,标准等)。


< snip>

while(begin< end)
{
if(* begin!= *结束)
返回false;
开始++;
结束 - ;




刚认识到这应该是:

++开始;

- 结束;


Joe


< blockquote> Joe Van Dyk写道:

这是我对Accelerated C ++问题5-10的回答。我很感激
任何评论(关于风格,效率,标准等)的评论。

由此产生的程序似乎相当快,通过
在我的PowerBook G4上3秒内300,000+单词的词典。

//编写一个程序,找到字典中的所有回文,
//并找到最大的回文。
cat main.cc
//编写一个程序,在字典中找到所有的回文

//并找到最大的回文。


#include< iostream>

#include< vector>

bool is_palindrome(const std :: string& input){

std :: string :: const_iterator begin = input.begin();

std :: string :: const_iterator end = input.end() - 1;


while(开始< end){

if(* begin!= * end)

返回false;

++开始;

--end;

}

返回true;

}


bool compare_word_length(const std :: string& a,

const std :: string& b){

return(a .size()< b.size());

}


int main(int argc,char * argv []){

std :: cout<< std :: endl<<

"这个程序将在字典中查找并打印所有回文项目\ n>

"然后查找并打印\ n

最小和最大的那些。 << std :: endl;


std :: string input;

std :: vector< std :: string>回文;


//得到回文

而(std :: cin>>输入)

if(is_palindrome) (输入))

palindromes.push_back(输入);


//显示回文

std :: cout< < std :: endl;

std :: cout<< 回文是:\ n;;

for(std :: vector< std :: string> :: const_iterator iter =

palindromes.begin(); iter!= palindromes.end(); ++ iter){

std :: cout<< * iter<< ''\t'';

}

std :: cout<< std :: endl;


//排序回文

sort(palindromes.begin(),

palindromes.end() ,compare_word_length);


//显示最短和最长的一个

std :: string smallest = * palindromes.begin();

std :: string largest = *(palindromes.end() - 1);


std :: cout<< std :: endl;

std :: cout<< 最短的回文是'' <<最小的

<< ''和最长的一个是''' <<最大的<< "''。" ;;


std :: cout<< std :: endl;


返回0;

}

g ++ -Wall -ansi -pedantic -o main main.cc
时间./main< / usr / share / dict / words


这个程序将在字典中查找并打印所有的回文

然后找到并打印

是最小和最大的。


回文是:

bib bob boob civic dad deed do dud \

e e e e e eye eye \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ >
性别独奏山雀


最短的回文是''山雀''\\ n \\ n

而最长的回文是''旋转''。

0.303u 0.007s 0:00.31 96.7%0 + 0k 0 + 0io 0pf + 0w wc / usr / share / dict / words



45427 45427 409305 / usr / share / dict / words




" Joe Van Dyk" <乔******* @ nospam.gmail.com>在消息中写道

新闻:2004101617264416807%joevandyk @ nospamgmailcom。 ..

这是我对Accelerated C ++问题5-10的回答。我很感激
任何评论(关于风格,效率,标准等)你的伙伴


基本上对我来说很好。但是后面会有一两个建议。

结果程序似乎相当快,在我的PowerBook G4上用3秒内的300,000+单词搜索。

//编写一个程序,找到字典中的所有回文,
//并找到最大的回文。

#include< iostream>
#include< vector> ;

使用std :: cout;使用std :: cin;
使用std :: string;使用std :: endl;
使用std :: vector;

bool is_palindrome(const string&);
bool compare_word_length(const string&,const string&);

int main()
{
cout<< 该程序将在字典中找到所有的回文并且将找到最大的回文。 << std :: endl;

字符串输入;
vector< string>回文;

//得到回文
while(cin>>输入)
if(is_palindrome(输入))
palindromes.push_back(输入);

//显示回文
cout<< \ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ )
{
cout<< * iter<< ''\t'';
}
cout<< \\\
;


您可以将此视为上述循环的替代品


copy(palindromes.begin(),palindromes.end(),ostream_iterator (cout,

" \t"));

cout<< ''\ n'';


你需要包含< algorithm>用于复制,和< iterator>,用于

ostream_iterator。

//排序回文
sort(palindromes.begin(),palindromes.end,compare_word_length
//显示最短和最长的一个
string smallest = * palindromes.begin();
string largest = *(palindromes.end() - 1);


我会添加一张支票,你至少有一个回文,否则你将会崩溃,我会使用front()和back()


if(!palindromes.empty())

{

string smallest = palindromes.front();

string largest = palindromes.back();

...

}

cout<<" \ n最短的回文是''"<<<<<<<<"'""
"最长的一个是''"<<最大<<"'\\' n" ;;

cout<< endl;

返回0;
}

bool is_回文(const string& input)
{
string :: const_iterator begin,end;
begin = input.begin end = i nput.end-1


我认为结合声明和初始化更好的风格,在某些情况下,它也更有效率。


string :: const_iterator begin = input.begin();

string :: const_iterator end = input.end()-1;

while(begin<结束)
{
if(* begin!= * end)
return false;
begin ++;
end--;
}
返回true;
}

bool compare_word_length(const string& a,const string& b)
{
return(a.size()< b .size());
}




john


This is my answer to problem 5-10 from Accelerated C++. I''d appreciate
any comments (regarding style, efficiency, standards, etc) you fellows
have.

The resulting program seems reasonably fast, searching through a
dictionary of 300,000+ words in 3 seconds on my PowerBook G4.

// Write a program that finds all the palindromes in a dictionary,
// and also find the largest palindrome.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::string; using std::endl;
using std::vector;

bool is_palindrome(const string&);
bool compare_word_length(const string &, const string &);

int main()
{
cout << "This program will find all the palindromes in a dictionary "
"and will find the largest one." << std::endl;

string input;
vector<string> palindromes;

// Get the palindromes
while (cin >> input)
if (is_palindrome(input))
palindromes.push_back(input);

// Display the palindromes
cout << "\nThe palindromes were:\n";
vector<string>::const_iterator iter;
for (iter = palindromes.begin(); iter != palindromes.end(); ++iter)
{
cout << *iter << ''\t'';
}
cout << ''\n'';

// Sorting palindromes
sort (palindromes.begin(), palindromes.end(), compare_word_length);

// Display the shortest and longest one
string smallest = *palindromes.begin();
string largest = *(palindromes.end()-1);

cout << "\nThe shortest palindrome was ''" << smallest << "''"
" and the longest one was ''" << largest << "''\n";

cout << endl;

return 0;
}

bool is_palindrome(const string &input)
{
string::const_iterator begin, end;
begin = input.begin();
end = input.end() - 1;

while (begin < end)
{
if (*begin != *end)
return false;
begin++;
end--;
}
return true;
}

bool compare_word_length(const string &a, const string &b)
{
return (a.size() < b.size());
}

解决方案

On 2004-10-16 17:26:45 -0700, Joe Van Dyk <jo*******@nospam.gmail.com> said:

This is my answer to problem 5-10 from Accelerated C++. I''d appreciate
any comments (regarding style, efficiency, standards, etc) you fellows
have.
<snip>


while (begin < end)
{
if (*begin != *end)
return false;
begin++;
end--;



Just realized this should be:
++begin;
--end;

Joe


Joe Van Dyk wrote:

This is my answer to problem 5-10 from Accelerated C++. I''d appreciate
any comments (regarding style, efficiency, standards, etc) you fellows
have.

The resulting program seems reasonably fast, searching through a
dictionary of 300,000+ words in 3 seconds on my PowerBook G4.

// Write a program that finds all the palindromes in a dictionary,
// and also find the largest palindrome. cat main.cc // Write a program that finds all the palindromes
// in a dictionary and also find the largest palindrome.

#include <iostream>
#include <vector>

bool is_palindrome(const std::string& input) {
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end() - 1;

while (begin < end) {
if (*begin != *end)
return false;
++begin;
--end;
}
return true;
}

bool compare_word_length(const std::string &a,
const std::string &b) {
return (a.size() < b.size());
}

int main(int argc, char* argv[]) {
std::cout << std::endl <<
"This program will find and print all the palindromes\n"
"in a dictionary then finds and prints\n"
"the the smallest and largest ones." << std::endl;

std::string input;
std::vector<std::string> palindromes;

// Get the palindromes
while (std::cin >> input)
if (is_palindrome(input))
palindromes.push_back(input);

// Display the palindromes
std::cout << std::endl;
std::cout << "The palindromes were:\n";
for (std::vector<std::string>::const_iterator iter =
palindromes.begin(); iter != palindromes.end(); ++iter) {
std::cout << *iter << ''\t'';
}
std::cout << std::endl;

// Sorting palindromes
sort(palindromes.begin(),
palindromes.end(), compare_word_length);

// Display the shortest and longest one
std::string smallest = *palindromes.begin();
std::string largest = *(palindromes.end() - 1);

std::cout << std::endl;
std::cout << "The shortest palindrome was ''" << smallest
<< "'' and the longest one was ''" << largest << "''.";

std::cout << std::endl;

return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
time ./main < /usr/share/dict/words
This program will find and print all the palindromes
in a dictionary then finds and prints
the the smallest and largest ones.

The palindromes were:
bib bob boob civic dad deed did dud \
eke ere ewe eye gag gig huh level \
madam non noon nun peep pep pip pop \
pup radar redder refer reviver rotator rotor sees \
sexes solos tit

The shortest palindrome was ''tit'' \
and the longest one was ''rotator''.
0.303u 0.007s 0:00.31 96.7% 0+0k 0+0io 0pf+0w wc /usr/share/dict/words


45427 45427 409305 /usr/share/dict/words



"Joe Van Dyk" <jo*******@nospam.gmail.com> wrote in message
news:2004101617264416807%joevandyk@nospamgmailcom. ..

This is my answer to problem 5-10 from Accelerated C++. I''d appreciate
any comments (regarding style, efficiency, standards, etc) you fellows
have.

Basically looks fine to me. But one or two suggestions follow.
The resulting program seems reasonably fast, searching through a
dictionary of 300,000+ words in 3 seconds on my PowerBook G4.

// Write a program that finds all the palindromes in a dictionary,
// and also find the largest palindrome.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::string; using std::endl;
using std::vector;

bool is_palindrome(const string&);
bool compare_word_length(const string &, const string &);

int main()
{
cout << "This program will find all the palindromes in a dictionary "
"and will find the largest one." << std::endl;

string input;
vector<string> palindromes;

// Get the palindromes
while (cin >> input)
if (is_palindrome(input))
palindromes.push_back(input);

// Display the palindromes
cout << "\nThe palindromes were:\n";
vector<string>::const_iterator iter;
for (iter = palindromes.begin(); i ter!=palindromes.end ++iter)
{
cout << *iter << ''\t'';
}
cout << ''\n'';
You could consider this as a replacement for the above loop

copy(palindromes.begin (), palindromes.end (), ostream_iterator(cout,
"\t"));
cout << ''\n'';

You''d need to include <algorithm> for copy, and <iterator>, for
ostream_iterator.

// Sorting palindromes
sort (palindromes.begin(), palindromes.end,compare_word_length
// Display the shortest and longest one
string smallest = *palindromes.begin();
string largest = *(palindromes.end()-1);
I would add a check that you have at least one palindrome, otherwise you are
going to crash, and I''d use front() and back()

if (!palindromes.empty())
{
string smallest = palindromes.front();
string largest = palindromes.back();
...
}

cout << "\nThe shortest palindrome was ''" << smallest << "''"
" and the longest one was ''" << largest << "''\n";

cout << endl;

return 0;
}

bool is_palindrome(const string &input)
{
string::const_iterator begin, end;
begin = input.begin end = i nput.end-1
I think its better style to combine declaration and initialisation, in some
circumstances it is also more efficient.

string::const_iterator begin = input.begin ();
string::const_iterator end = input.end ()-1;

while (begin < end)
{
if (*begin != *end)
return false;
begin++;
end--;
}
return true;
}

bool compare_word_length(const string &a, const string &b)
{
return (a.size() < b.size());
}



john


这篇关于要求的代码审查,Accelerated C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆