c++ set输出问题

以下是我的代码

for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }

这样打印最后还是有一个空格 ,请问才能不打印最后的空格

for (set<int>::const_iterator it = s.begin(); it != s.end(); ++it) {
    if (it != s.begin())
        cout << ' ';
    cout << *it;
}