将字符串拆分为向量;的话
从加速C++(书)中,我发现这段代码是相同的程序,但程序本身的处理不同,这让我在某些方面感到困惑。
下面的代码,很明显,它会在用户包含文件结束符后根据用户输入逐一(通过循环)输出每个单词,然后结束程序。
int main()
{
string s;
while (cin >> s)
cout << s << endl;
return 0;
}
与上面的代码不同,此代码会将每个单词存储在一个向量中,然后使用索引 i 和 j 来检测非空白字符,并且真正的问题是,我不明白向量是如何发生的。
向量
中的空白是什么?一个元素?
起初,我认为程序将遍历每个字符,因为我认为空格是字符(其中i
和j< /code>功能是为了),然后,书上说它会通过每个单词进行,我不知道如何自己测试这个,就像我可以看到编译器中的内部过程一样本身..
vector<string> split(const string& s)
{
vector<string> ret;
typedef string::size_type string_size;
string_size i = 0;
// invariant: we have processed characters [original value of i, i)
while (i != s.size())
{
// ignore leading blanks
// invariant: characters in range [original i, current i) are all spaces
while (i != s.size() && isspace(s[i]))
++i;
// find end of next word
string_size j = i;
// invariant: none of the characters in range [original j, current j)is a space
while (j != s.size() && !isspace(s[j]))
j++;
// if we found some nonwhitespace characters
if (i != j) {
// copy from s starting at i and taking j - i chars
ret.push_back(s.substr(i, j - i));
i = j;
}
}
return ret;
}
int main() {
string s;
// read and split each line of input
while (getline(cin, s)) {
vector<string> v = split(s);
// write each word in v
for (vector<string>::size_type i = 0; i != v.size(); ++i)
cout << v[i] << endl;
}
return 0;
}
From Accelerated C++(book), I found this code which is identical program, but the processed in program itself is different, and confused me on some part.
The code below, well, obviously it will output each word one-by-one(by loops) based on user input after the user included end-of-file, then, end the program.
int main()
{
string s;
while (cin >> s)
cout << s << endl;
return 0;
}
Unlike code above, this one will store each word in a vector
, then use index i
and j
to detect the non-whitespace character, and the real question is, I don't understand how it happens with the vector.
What is whitespace in vector
? An element?
At first, I thought the program will proceed through each character, because I thought the whitespace is character(which i
and j
functionality is for), then, the book come and said it proceed through each word, I don't know how to test this myself, like I can see how the inner process in the compiler itself..
vector<string> split(const string& s)
{
vector<string> ret;
typedef string::size_type string_size;
string_size i = 0;
// invariant: we have processed characters [original value of i, i)
while (i != s.size())
{
// ignore leading blanks
// invariant: characters in range [original i, current i) are all spaces
while (i != s.size() && isspace(s[i]))
++i;
// find end of next word
string_size j = i;
// invariant: none of the characters in range [original j, current j)is a space
while (j != s.size() && !isspace(s[j]))
j++;
// if we found some nonwhitespace characters
if (i != j) {
// copy from s starting at i and taking j - i chars
ret.push_back(s.substr(i, j - i));
i = j;
}
}
return ret;
}
int main() {
string s;
// read and split each line of input
while (getline(cin, s)) {
vector<string> v = split(s);
// write each word in v
for (vector<string>::size_type i = 0; i != v.size(); ++i)
cout << v[i] << endl;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您上面发布的代码不会根据空格将一行文本拆分为单词,而是将一行文本拆分为字符。但是,前提是代码实际上是可编译的并且没有缺少任何必要的大括号(
{
、}
)。 编辑:实际上,它是否分割单词或单个字符取决于大括号的位置,底线是代码无法编译。以下是代码的固定版本,只需移动
中的最后一个
在它的直接if
语句即可拆分每个单词,而不是每个字符 splitwhile
块之外:传递给
split
的string
会发生什么:while (i != s.size() && isspace(s[i]))
++i
)string_size j = i
)while (j != s.size() && !isspace(s[j]))
)j++
)if (i != j)
)s.substr(i, j - i)
),并将该单词添加到向量< /code> (
ret.push_back(..)
).The code you posted above does not split a line of text into words, based on whitespace, it instead splits a line into characters. However, that's if the code was actually compilable and not missing any necessary braces (
{
,}
). EDIT: Actually whether it splits words or individual characters depends on where the braces go, bottom line is that the code doesn't compile.Here is a fixed version of the code that splits each word, rather than each character, by simply moving the last
if
statement insplit
outside of it's immediatewhile
block:What happens to the
string
passed tosplit
is:while (i != s.size())
)while (i != s.size() && isspace(s[i]))
++i
)string_size j = i
)while (j != s.size() && !isspace(s[j]))
)j++
)if (i != j)
)s.substr(i, j - i)
), and add that word to thevector
(ret.push_back(..)
).如果您只是根据空间进行分割,那么您不需要编写自定义方法。 STL 为您提供多种选择。
If you are just splitting based on space, then you don't need write a custom method. STL has options for you.