题解 | #单词倒排#

单词倒排

http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836

读入一行字符串,正则遍历找出符合要求的子串,压入栈,输出(出栈)直到栈空。

#include <iostream>
#include <string>
#include <regex>
#include <stack>
using namespace std;
int main ()
{
    string str; 
    stack<string> tmp;
    getline(cin,str);
    regex regstring("[A-Za-z]+");
    for(sregex_iterator it(str.begin(),str.end(),regstring),it_end; it!=it_end; ++it)    
    {
        tmp.push(it -> str());   //将符合要求的字符串压入栈 
    }
    while(!tmp.empty())
    {
        cout<<tmp.top()<<' '; //栈反向输出
        tmp.pop();  //pop-1 
    }
    return 0;
}
全部评论

相关推荐

自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 14:08
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务