函数打印一个集合
C++ function to print a set
有没有人知道为什么
#include <set>
#include <string>
#include <iostream>
template <typename T>
std::string set2Str(const std::set<T> & S)
{
std::string retstr = "{";
typename std::set<T>::const_iterator it(S.begin()), offend(S.end());
if (it != offend)
retstr.push_back(*it++);
while (it != offend)
{
retstr.push_back(',');
retstr.push_back(*it++);
}
retstr.push_back('}');
return retstr;
}
int main()
{
std::set<int> mySet = {1, 5, 9, 69};
std::cout << set2Str(mySet);
}
输出是{,, ,E}
? ?
还有,是否有更优雅的方式来写函数set2Str
?逗号的栅栏问题使我的程序很难看。
当你的算法这样做时:
retstr.push_back(*it++);
被打孔到目标字符串中的值被处理为(如果可能的话转换为)char
。但{1, 5, 9, 69}
不含char
;它包含int
。结果是将它们视为ASCII码点,请参阅此表,并特别注意其中每个字符的dec
值。例如,注意E
的值。
这是std::ostringstream
的众多用途之一,它的好处是允许任何可以写入字符流的都是可表示的,包括使用自定义插入操作符。
#include <iostream>
#include <sstream>
#include <string>
#include <set>
#include <tuple>
template<typename T, typename... Args>
std::string set2str(const std::set<T,Args...>& obj)
{
std::ostringstream oss;
oss << '{';
auto it = obj.cbegin();
if (it != obj.cend())
{
oss << *it++;
while (it != obj.cend())
oss << ',' << *it++;
}
oss << '}';
return oss.str();
}
// custom class to demonstrate custom insertion support
class Point
{
friend std::ostream& operator <<(std::ostream& os, const Point& pt)
{
return os << '(' << pt.x << ',' << pt.y << ')';
}
private:
double x,y;
public:
Point(double x, double y) : x(x), y(y) {}
// used by std::less<> for set ordering
bool operator <(const Point& pt) const
{
return std::tie(x,y) < std::tie(pt.x,pt.y);
}
};
int main()
{
std::set<int> si = { 1,2,3,4,5 };
std::set<double> sd = { 1.1, 2.2, 3.3, 4.4, 5.5 };
std::set<char> sc = { 'a', 'b', 'c', 'd', 'e' };
std::set<unsigned> empty;
std::cout << set2str(si) << 'n';
std::cout << set2str(sd) << 'n';
std::cout << set2str(sc) << 'n';
std::cout << set2str(empty) << 'n';
// using custom class with implemented ostream inseter
std::set<Point> pts { {2.2, 3.3}, {1.1, 2.2}, {5.5, 4.4}, {1.1, 3.3} };
std::cout << set2str(pts) << 'n';
return EXIT_SUCCESS;
}
{1,2,3,4,5}
{1.1,2.2,3.3,4.4,5.5}
{a,b,c,d,e}
{}
{(1.1,2.2),(1.1,3.3),(2.2,3.3),(5.5,4.4)}
您需要将*it++
转换为字符串—例如使用std::to_string
。否则,它可能是一个字符,并被打印出来。
例如,在您的情况下,您只需添加69
,这是e的Ascii码。更可能的是,您想要"69"
,您将获得std::to_string(69)
。
您声明该类型为整型。这些数字被读取为ASCII码。它们没有被转换为字符串类型。
retstr.push_back (* + +);
这一行看到的是ANSI/ASCII大写字母E,而不是字符'6'和' 5'
使用你可能不喜欢的sprintf方法
将整型push_back转换为字符串是你的问题。
实际上,如果您的类型不是int类型,那么这将不起作用,因此您需要检测所使用的类型。或者更好的方法是使用stringstream并以这种方式进行转换。
#include <set>
#include <string>
#include <iostream>
template <typename T>
std::string set2Str(const std::set<T> & S)
{
std::string retstr = "{";
char buffer[20] = {0};
typename std::set<T>::const_iterator it(S.begin()), offend(S.end());
if (it != offend) {
sprintf(buffer, "%u", *it++);
retstr.append(buffer);
}
while (it != offend)
{
retstr.push_back(',');
sprintf(buffer, "%u", *it++);
retstr.append(buffer);
}
retstr.push_back('}');
return retstr;
}
int main()
{
std::set<int> mySet; // // = {1, 5, 9, 69}; - didn't work with my compiler
mySet.insert(1);
mySet.insert(5);
mySet.insert(9);
mySet.insert(69);
std::cout << set2Str(mySet);
}
相关文章:
- 大三实现在一个集合类
- (也许是NP-Hard)求一个集合的子集总数,使得每个子集在与其所有元素相乘时的值都大于X
- 维护多集中数字的排序列表和另一个集合中的累积总和
- std::set的替代品(可以将元素从一个集合移动到另一个集合)
- 如果您在c++中更新一个集合的成员,该集合会自动更改顺序吗
- 如何制作一个集合,其中元素使用局部变量进行排序
- 增强 MPL 将每个元素包装在一个集合中
- 我怎样才能插入到一个集合和一个德克
- C++在一个集合中存储多个数据类型
- 如何在C++中检查一个集合是否有某个范围内的元素
- 计算一个集合中不同于另一个集合的元素数
- 当返回一个集合指针时,客户端如何知道它是否需要销毁它?
- 从一个集合生成所有可能的有序子集
- 如何检验一个集合是否自反、对称、反对称和/或传递
- 如何将一个集合分成K个子集,使得子集中元素的和最小
- 将unique_ptr从一个集合移动到另一个集合
- 在c++中反转一个集合的内容
- 对于每个模板类型,都有一个集合类型的实参
- 生成一个集合的所有子集,使它们的集合等于整个集合
- 一个集合是否可能包含指向同一对象的两个共享指针?