tommyyu 发表于 2022-8-8 12:31:25

如何用scanf函数输入string字符串

请问如何使用scanf函数输入string字符串

一点点儿 发表于 2022-8-8 12:45:26

string s;
s.resize(100);
scanf("%s",&s);

这样可以

柿子饼同学 发表于 2022-8-8 13:13:46

建议用cin

tommyyu 发表于 2022-8-8 13:14:36

柿子饼同学 发表于 2022-8-8 13:13
建议用cin

可是cin没有返回值,不能while(cin>>x == 1)这样用

柿子饼同学 发表于 2022-8-8 13:33:24

tommyyu 发表于 2022-8-8 13:14
可是cin没有返回值,不能while(cin>>x == 1)这样用

可是
这是字符串啊

tommyyu 发表于 2022-8-8 15:52:34

柿子饼同学 发表于 2022-8-8 13:33
可是
这是字符串啊

用空格/换行隔开的字符串

柿子饼同学 发表于 2022-8-8 19:04:29

tommyyu 发表于 2022-8-8 15:52
用空格/换行隔开的字符串

啥题目 , 我康康

tommyyu 发表于 2022-8-8 19:41:58

柿子饼同学 发表于 2022-8-8 19:04
啥题目 , 我康康

传送门

柿子饼同学 发表于 2022-8-8 20:03:56

tommyyu 发表于 2022-8-8 19:41
传送门

有空格用 getline 吧
不过你的方法是一次读入一个单词 , 看是否相等吧 , 那还是循环吧

tommyyu 发表于 2022-8-8 20:07:03

柿子饼同学 发表于 2022-8-8 20:03
有空格用 getline 吧
不过你的方法是一次读入一个单词 , 看是否相等吧 , 那还是循环吧

好的

人造人 发表于 2022-8-8 22:03:06

本帖最后由 人造人 于 2022-8-8 22:09 编辑

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <regex>

using std::cin, std::cout, std::endl;
using std::for_each, std::getline;
using std::string, std::vector;
using std::regex, std::smatch;
using std::sregex_iterator;

int main() {
    string word; getline(cin, word);
    string text; getline(cin, text);
    regex pattern("\\b" + word + "\\b", regex::icase);
    vector<smatch> result;
    for_each(sregex_iterator(text.begin(), text.end(), pattern), sregex_iterator(), [&result](const smatch &s) -> void {result.push_back(s);});
    if(!result.size()) cout << -1 << endl;
    else cout << result.size() << " " << result.position() << endl;
    return 0;
}

tommyyu 发表于 2022-8-10 18:17:58

人造人 发表于 2022-8-8 22:03


看不懂啊{:10_266:}

人造人 发表于 2022-8-10 18:33:01

tommyyu 发表于 2022-8-10 18:17
看不懂啊

哪里看不懂?

tommyyu 发表于 2022-8-10 18:37:22

人造人 发表于 2022-8-10 18:33
哪里看不懂?

我基本上全看不懂{:10_266:}
还得多学
页: [1]
查看完整版本: 如何用scanf函数输入string字符串