简体   繁体   English

c ++基本字符串操作

[英]c++ basic string manipulation

There's a simple string manipulation problem where you are required to reverse the words of each line: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=121&page=show_problem&problem=424 有一个简单的字符串操作问题,你需要反转每行的单词: http//uva.onlinejudge.org/index.php?option = com_onlinejudge&Itemid = 8&category = 121&page = show_problem&issue424

So: 所以:

I love you.
You love me.
We're a happy family.

Would become: 会成为:

I evol .uoy
uoY evol .em
er'eW a yppah .ylimaf

Now, I've written a simple java solution which looks something like: 现在,我编写了一个简单的java解决方案,它看起来像:

BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
String s;
while((s=file.readLine())!=null){
    String[] sr = s.split(" ");
    for(int i = 0; i<sr.length; i++)
        System.out.print(new StringBuffer(sr[i]).reverse() + (i==sr.length-1?"\n":" "));
}

Because I am trying to learn c++, I've also tried writing a c++ solution which looks like: 因为我正在尝试学习c ++,所以我也尝试编写一个c ++解决方案,它看起来像:

string s;
while(getline(cin, s)){
    string tmp = "";
    for(int i = 0; i<=s.length(); i++)
        if( i==s.length() || s[i] == ' '){
            for(int j = tmp.length(); j>=0; j--)
                cout << tmp[j];
            if( i == s.length()) cout << endl;
            else cout << " ";
            tmp = "";
        }else
            tmp += s[i];
}

My questions are: 我的问题是:

  1. The c++ solution returns "wrong answer", whereas the java one is accepted, why? c ++解决方案返回“错误答案”,而java一个被接受,为什么?
  2. What improvements, if any, can be made to the c++ solution? 如果有的话,可以对c ++解决方案进行哪些改进?
std::string line_string;
while ( getline( std::cin, line_string ) ) {

    // Instead of `split`, read out of a std::istringstream:
    std::istringstream line_stream( line_string );
    std::string word;
    while ( line_stream >> word ) {

        // Use std::reverse instead of a loop:
        std::reverse( word.begin(), word.end() );

        // Always qualify with std:: instead of using namespace std;
        std::cout << word << ' ';
    }
    std::cout << '\n'; // prefer '\n' to std::endl unless you need a flush
}

http://ideone.com/hd3bg http://ideone.com/hd3bg

If this doesn't pass, it's probably because of the trailing space at the end of each line. 如果没有通过,可能是因为每行末尾的尾随空格。 Use a Boolean variable to avoid printing space before newline. 使用布尔变量以避免在换行前打印空间。

If boost is allowed, I would use a combination of boost::split , boost::join and std::reverse . 如果允许boost,我会使用boost::splitboost::joinstd::reverse

std::string line;
std::vector<std::string> vs;
while (getline(std::cin, line)) {       
    boost::split(vs, line, ::isspace);
    for (auto & word : vs)
        std::reverse(word.begin(), word.end());
    std::cout << boost::join(vs, " ") << '\n';
}

If boost is not available, I would have those functions (or similar) in my personal library and I would just copy them into my submission. 如果没有提升,我会在我的个人图书馆中拥有这些功能(或类似功能),我会将它们复制到我的提交中。

the reason of wrong is: 错误的原因是:

string s;
while(getline(cin, s)){
    string tmp = "";
    for(int i = 0; i<=s.length(); i++)
        if( i==s.length() || s[i] == ' '){
            for(int j = tmp.length(); j>=0; j--)
                        ^^^^^^^^^^^^ => the first tmp[j] is \0!
                                     => should be tmp.length()-1
                cout << tmp[j];
            if( i == s.length()) cout << endl;
            else cout << " ";
            tmp = "";
        }else
            tmp += s[i];
}

remember, in c/c++, an index starts from 0. 记住,在c / c ++中,索引从0开始。

If you absolutly want to create a new variable to handle the reverse operation instead of simple printing in the reverse order. 如果你绝对想要创建一个新变量来处理反向操作而不是以相反的顺序进行简单打印。 Then you have to take in consideration the fallowing : In C++ the index is not the length of a string. 然后你必须考虑到以下情况:在C ++中,索引不是字符串的长度。 Because the index starts at 0, so you start from the index 0 to the index : length - 1 因为索引从0开始,所以你从索引0开始到索引:length - 1

If you want to start C++, after you're familiar with the basics it's always good to digg into the STL before going anywhere else (that's just my opinion). 如果你想要开始使用C ++,那么在熟悉基础知识之后,最好先去STL,然后去其他地方(这只是我的意见)。

Here is some code : 这是一些代码:

string s;  
while(getline(cin, s))
{      
   string tmp = "";      
   for(int i = s.length() - 1; i >= 0; i++)
       std::cout<<s[i];
}

I think if I was going to do this, I'd reverse each word by constructing a new string, passing word.rbegin(), word.rend() as the arguments to the constructor. 我想如果我要这样做,我会通过构造一个新字符串来反转每个单词,传递word.rbegin(), word.rend()作为构造函数的参数。

Using the Line class from one of my previous answers, and the infix_ostream_iterator from another, it could look something like this: 使用我之前的一个答案中的Line和另一个的infix_ostream_iterator ,它看起来像这样:

#include "infix_iterator.h"
#include "line"


int main() {
    auto rev_words = [](std::string const &in) -> std::string {
        std::istringstream b(in);
        std::ostringstream r;

        std::transform(std::istream_iterator<std::string>(b),
            std::istream_iterator<std::string>(),
            infix_ostream_iterator<std::string>(r, " "),
            [](std::string const &in){return std::string(in.rbegin(), 
                                                         in.rend());});
        return r.str();
    };

    std::transform(std::istream_iterator<line>(std::cin), 
        std::istream_iterator<line>(), 
        std::ostream_iterator<std::string>(std::cout, "\n"),
        rev_words);
    return 0;
}

Yes, this is probably going a bit overboard on fully-functional style. 是的,这可能在全功能风格上有点过分。 It's undoubtedly easier to do something like this instead: 相反,做这样的事情无疑更容易:

std::string line;
while (std::getline(std::cin, line)) {
    std::istringstream buffer(line);
    std::transform(std::istream_iterator<std::string>(buffer),
                   std::istream_iterator<std::string>(),
                   infix_ostream_iterator<std::string>(std::cout , " "), 
                   [](std::string const &in){return std::string(in.rbegin(), 
                                                                in.rend()););
    std::cout << "\n";
}

Here the one i tried. 这是我试过的那个。

#include <iostream>
#include <string>

 using namespace std;


 int main(){

    string s = "Hello How are you Mr vins what are you doing now";
    string temp, reverse;

   int i,j;
     // Traverse through First string
    for (i=0;i <=s.length(); i++)
   {
if(s[i] == ' ' || s[i] == '\0') {
    //After getting space reverse the previous word and add to reverse string
    for (j = temp.length(); j >=0; j--)
        reverse = reverse + temp[j];

    reverse = reverse + " ";
    temp="";
    continue;
}
temp = temp + s[i];

}

 cout << "\nlets see reversed sentence \n";

   cout << reverse << "\n";
      return 0;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM