为什么 std::cout 在我的输出中添加 return 或换行符?
Why is std::cout adding return or newlines to my output?
使用 stringstream
从句子中提取单词。
每个单词都加了一个换行符,我不明白为什么;你是否可以?感谢您的帮助。基思:^)
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
int main() {
int wordCount(9);
std::vector<std::string> v(wordCount);
std::vector<std::string>::iterator it;
std::string tStr;
std::string myStr = "the quick fox jumped over the lazy brown dogs";
std::stringstream ss(myStr);
std::cout << "Sentence broken into vector:" << std::endl;
for (int i=0; i<wordCount; ++i) {
ss >> tStr;
v.push_back(tStr);
}
for (it=v.begin(); it != v.end(); ++it)
std::cout << *it << std::endl;
std::cout << std::endl;
return 0;
}
编译,运行,输出。注意额外的换行符。
pickledEgg $ g++ -std=c++11 -g -Og -o io2 io2.cpp
pickledEgg $ ./io2
Sentence broken into vector:
the
quick
fox
jumped
over
the
lazy
brown
dogs
当您使用此行创建矢量时 std::vector<std::string> v(wordCount);
您创建了 wordCount
个空条目。当您调用 push_back
添加您的单词时,您将单词附加在向量的末尾。当你迭代你的向量时,你首先用新行打印空条目,然后是你的好数据。
相当简单:
//std::vector<std::string> v(wordCount);
std::vector<std::string> v;
我想你想使用 std::vector::reserve
。
The elements are stored contiguously, which means that elements can be
accessed not only through iterators, but also using offsets on regular
pointers to elements. This means that a pointer to an element of a
vector may be passed to any function that expects a pointer to an
element of an array.
http://en.cppreference.com/w/cpp/container/vector
因为 std::vector::push_back
将值附加到矢量中,导致其大小发生变化。如果你处理一个足够大的向量,你应该考虑使用 std::vector::reserve
.
您可以在 http://en.cppreference.com/w/cpp/container/vector/reserve 中看到差异。
使用 stringstream
从句子中提取单词。
每个单词都加了一个换行符,我不明白为什么;你是否可以?感谢您的帮助。基思:^)
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
int main() {
int wordCount(9);
std::vector<std::string> v(wordCount);
std::vector<std::string>::iterator it;
std::string tStr;
std::string myStr = "the quick fox jumped over the lazy brown dogs";
std::stringstream ss(myStr);
std::cout << "Sentence broken into vector:" << std::endl;
for (int i=0; i<wordCount; ++i) {
ss >> tStr;
v.push_back(tStr);
}
for (it=v.begin(); it != v.end(); ++it)
std::cout << *it << std::endl;
std::cout << std::endl;
return 0;
}
编译,运行,输出。注意额外的换行符。
pickledEgg $ g++ -std=c++11 -g -Og -o io2 io2.cpp
pickledEgg $ ./io2
Sentence broken into vector:
the
quick
fox
jumped
over
the
lazy
brown
dogs
当您使用此行创建矢量时 std::vector<std::string> v(wordCount);
您创建了 wordCount
个空条目。当您调用 push_back
添加您的单词时,您将单词附加在向量的末尾。当你迭代你的向量时,你首先用新行打印空条目,然后是你的好数据。
相当简单:
//std::vector<std::string> v(wordCount);
std::vector<std::string> v;
我想你想使用 std::vector::reserve
。
The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets on regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array. http://en.cppreference.com/w/cpp/container/vector
因为 std::vector::push_back
将值附加到矢量中,导致其大小发生变化。如果你处理一个足够大的向量,你应该考虑使用 std::vector::reserve
.
您可以在 http://en.cppreference.com/w/cpp/container/vector/reserve 中看到差异。