#include <iostream>
#include <string>
#include <sstream>
using namespace std;
std::wstring inputfield;
void function(uint32_t val)
{
std::wstring keystring;
keystring = std::to_wstring(val);
inputfield = inputfield + keystring;
std::wstringstream s;
s << L"(" << inputfield << L") ";
std::wstring str = s.str();
std::wcout << str << "\n";
return;
}
int main()
{
uint32_t x ;
while(cin>>x)
{
function(x);
cout<< *(&x)<< endl;
}
return 0;
}我正在尝试字符串格式,以获得类似(000)-000-000或美国电话号码格式的输出,但我无法实现,请帮助,谢谢
发布于 2017-02-19 04:17:12
如果您想更改字符串000000000或simillar (您确实这样做了,即使输入是数字,也只需使用std::to_string将其转换),那么只需在适当的位置添加几个字符即可。
std::string format_number(std::string str) {
if(str.size() != 9) {
throw std::logic_error{"Phone number is not 9-characters long"};
}
str.insert(str.begin(), '(');
str.insert(str.begin() + 4, ')');
str.insert(str.begin() + 5, '-');
str.insert(str.begin() + 9, '-');
return str;
}发布于 2017-02-19 04:23:33
你可以分配输入的电话号码,然后像这样传递结构;
struct numbers {
int A;
int B;
int C;
} // then get (A)-B-C 发布于 2020-05-19 18:02:10
#include <iostream>
#include <thread>
#include <string>
//---- The following function will be invoked by the thread library
void thread_proc(std::string msg)
{
std::cout << "ThreadProc msg:" << msg;
}
int main()
{
// creates a new thread and execute thread_proc on it.
std::thread t(thread_proc, "Hello World\n");
// Waiting for the thread_proc to complete its execution
// before exiting from the program
t.join();
}
How to resolve this error
$ g++ Dummy.cpp -o Dummy -std=c++1z -pthread
Dummy.cpp: In function 'int main()':
Dummy.cpp:12:10: error: 'thread' is not a member of 'std'
12 | std::thread t(thread_proc, "Hello World\n");
| ^~~~~~
Dummy.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
2 | #include <thread>
+++ |+#include <thread>
3 | #include <string>
Dummy.cpp:15:5: error: 't' was not declared in this scope; did you mean 'tm'?
15 | t.join();
| ^
| tmhttps://stackoverflow.com/questions/42319907
复制相似问题