C++ C++;字符串插入迭代
我有字符串:C++ C++;字符串插入迭代,c++,string,C++,String,我有字符串: string str = "1234567890"; //Magic code cout<<str<<endl; string str=“1234567890”; //魔码 cout没有你想要的内置功能。 相反,最方便的解决方案可能是遍历字符串并在执行时输出成对的数字: string str = "1234567890"; for (auto it = str.begin(); it != str.end(); ++it){ std::cout
string str = "1234567890";
//Magic code
cout<<str<<endl;
string str=“1234567890”;
//魔码
cout没有你想要的内置功能。
相反,最方便的解决方案可能是遍历字符串并在执行时输出成对的数字:
string str = "1234567890";
for (auto it = str.begin(); it != str.end(); ++it){
std::cout << *(it);
if (++it != str.end()){
std::cout << *it << " ";
}
}
std::cout << std::endl;
string str=“1234567890”;
对于(自动it=str.begin();it!=str.end();+it){
std::cout使用for
循环可以帮助您非常轻松地在std::string
中插入空格:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str = "1234567890";
for(auto it = str.begin(); it != str.end(); it += min<int>(str.end() - it, 2))
it = (it != str.begin() ? str.insert(it, ' ') + 1 : it);
cout << str << endl;
}
#包括
#包括
#include可以增加或减少一个以上。最小值(str.end()-it,2)
确保下一步不会越界。一种更通用的方法。定义一个函数,将给定的字符插入到给定的字符串s
中,每个间隔
字符,不包括字符串的开头和结尾:
std::string insert_char(const std::string& s, char char_to_insert, size_t interval)
{
// corner cases
if (!interval || !s.length()) return s;
// compute number of characters to insert
auto number_of_chars_to_insert = (s.length()-1)/interval;
// compute total length
auto output_length = s.length() + number_of_chars_to_insert;
// pre-allocate output string with all characters equal to char_to_insert
std::string retval(output_length, char_to_insert);
// cycle input string, keeping track of position in input and output strings
size_t pos = 0, pos_in_input = 0;
for (const auto& c : s)
{
// copy from input to output
retval[pos++] = c;
// advance in output every interval chars
if ((++pos_in_input) % interval == 0)
++pos;
}
return retval;
}
然后:
没有神奇的解决方案,我想说,使用简单的for循环很方便。那些重复的5
s是有意的吗?这可能会改变正确的答案很多。例如,你是想在每2个字符之间插入空格,还是想在某个神奇的连接处插入空格,在这种情况下,在相邻的奇数和偶数之间,同时也可以删除重复的字符……大概是答案。如果<代码> STR <代码>包含奇数个数字(非数字)、非图形字符、空格),然后你开始怀疑Unicode字符,并意识到C++仍然没有正式的Unicode字符串类:(protip:同时,使用glibmm
中的ustring
)您尝试了什么吗?您不需要*中的括号(它)
;*这就足够了。@PeteBecker啊,是的,我本打算“聪明”一点,在同一行的第二个字符中加入迭代器增量,但后来想得更好。当流式传输长度为1的字符串时,这是一种浪费。请改用char
。
std::string insert_char(const std::string& s, char char_to_insert, size_t interval)
{
// corner cases
if (!interval || !s.length()) return s;
// compute number of characters to insert
auto number_of_chars_to_insert = (s.length()-1)/interval;
// compute total length
auto output_length = s.length() + number_of_chars_to_insert;
// pre-allocate output string with all characters equal to char_to_insert
std::string retval(output_length, char_to_insert);
// cycle input string, keeping track of position in input and output strings
size_t pos = 0, pos_in_input = 0;
for (const auto& c : s)
{
// copy from input to output
retval[pos++] = c;
// advance in output every interval chars
if ((++pos_in_input) % interval == 0)
++pos;
}
return retval;
}
int main()
{
std::string s = "1234567890";
for (size_t i = 1; i != 5; ++i)
std::cout << insert_char(s, ' ', i) << std::endl;
return 0;
}
1 2 3 4 5 6 7 8 9 0
12 34 56 78 90
123 456 789 0
1234 5678 90