我对字符串流有一个小问题。当我用它将字符串转换为双精度时,它会失去精度。
const std::string str = "44.23331002";
double x;
stringstream ss;
ss << str;
ss >> x;
cout << str << " = " << x << endl;
Run Code Online (Sandbox Code Playgroud)
输出为: 44.23331002 = 44.2333
为什么是这样?它是否转换为浮点数并且数字精度有限?
你能给我看一些例子吗std::streampos?我不确定它的用途是什么,也不知道如何使用它。
我在github的一个项目中看到:
std::streampos pos = ss.tellg();
Run Code Online (Sandbox Code Playgroud)
哪里。ssstd::stringstream
int pos = ss.tellg()例如,在这种情况下我们为什么不使用, 呢?
我有一个代码库,它使用业务逻辑将 csv 文件转换为类似 xml 的配置文件。此代码大量使用字符串流来构造配置文件,其中一些文件中包含内联 SQL 语句。我正在考虑重构部分代码,并决定使用 libfmt 来构造配置字符串。与字符串流相比,代码肯定更具可读性,但我也希望性能有显着提升。
然而,我正在针对其他构造字符串的方法测试 libfmt 的简单基准,我发现即使与字符串流相比,它仍然很慢。(虽然上面的测试还包括 std::format (主要是出于好奇),但我们还没有迁移到 C++20,因此无法使用它。)
为什么会出现这种情况?我可以采取什么措施来改善它?
我已经查看了以下相关问题,但我的用例有所不同。为什么 {fmt} 比 std::stringstream 慢?
我尝试过的基准代码可以在这里找到:https ://godbolt.org/z/xrhPdarqa
我使用的框架已经在 CentOS 7 上运行了多年。我们正在将其迁移到 RHEL 8,但一些单元测试失败了。其中一个特别涉及从 std::runtime_error 上的 What() 返回垃圾。我创建了一个非常简单的例子来重复这个问题。它适用于 CentOS7,但不适用于 RHEL 8。代码如下:
#include <sstream>
#include <iostream>
const char * getString() {
std::ostringstream oss;
oss << "some stuff to return" << std::endl;
std::cout << "value to return: " << oss.str().c_str() << std::endl;
return oss.str().c_str();
}
int
main(int argc, char ** argv) {
std::string value = getString();
std::cout << "value returned: " << value << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
CentOS7 的输出是:
[user@localhost ~]$ ./a.out
value to return: some stuff to return …Run Code Online (Sandbox Code Playgroud) std::string str;
std::stringstream strm(str);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
错误11错误C2248:'std :: basic_ios <_Elem,_Traits> :: basic_ios':无法访问类'std :: basic_ios <_Elem,_Traits>'c:\ program files\microsoft visual studio 9.0\vc中声明的私有成员\ include\sstream 517
如果我使用istringstream,也会发生同样的情况.
编译器:Visual C++ 2008.
我写了这个程序:
#include <sstream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
double distance(vector<double> *, vector<double> *, int);
const char* distances(vector<vector<double>* >, int);
double distance(vector<double> a, vector<double> b){
double result = 0, di;
for(int i=0;i<a.size();i++){
di = a[i] - b[i];
result += di*di;
}
return sqrt(result);
}
const char* distances(vector<vector<double>* > vectors, int accuracy=1){
stringstream graphstr;
graphstr << "strict graph {\n";
for(int i=0; i<vectors.size();i++){
int j=i+1;
while(j<vectors.size()){
graphstr << "\t" << i << " -- " << j << "\t …Run Code Online (Sandbox Code Playgroud) 我已将som信息放入stringstream ss:
stringstream ss (stringstream::in | stringstream::out);
ss<<"abc 456 ";
ss<<123
Run Code Online (Sandbox Code Playgroud)
然后我决定将字符串内容检索到字符串g:
std::string s;
std::string g;
for (int n=0; n<c; n++)
{
ss >> s;
g=g+s;
}
cout << g <<endl;
Run Code Online (Sandbox Code Playgroud)
出于这个原因,我需要知道对ss进行了多少次放置.怎么知道?检索字符串信息的方法可能不是很聪明 - 然后给你自己的方式.
我有一段代码来读取构建日期和月份.这__DATE__是在预定义宏中定义的
const char* BUILD_DATE = __DATE__;
std::stringstream ss(BUILD_DATE);
std::string month;
size_t year;
ss >> month;
ss >> year;
ss >> year;
char buffer[1024];
sprintf(buffer, "Read year=[%d] month=[%s] date=[%s]", year,month.c_str(),BUILD_DATE);
Run Code Online (Sandbox Code Playgroud)
当它正常工作时,通常是缓冲区
读取年份= [2013]月= [3月]日期= [2013年3月9日]
但在某些运行中它是
读取年份= [0]月= [M]日期= [2013年3月9日]
要么
读取年份= [2013]月= [3月]日期= [2013年3月9日]
基本上年份是0或月份有额外的空间.
该项目是在Windows 7上使用Microsoft Visual Studio 2010 SP1的x64/CLR版本.
我很难过为什么偶尔会发生这种情况.我是否正确使用stringstream?
我尝试使用以下代码推广流对象:
#include <iostream>
#include <vector>
#include <sstream>
#include <iterator>
using namespace std;
template<class T, class U>
T& operator<<(T& os, vector<U> vec)
{
vector<string>::iterator begin = vec.begin();
vector<string>::iterator end = vec.end();
for (; begin != end; ++begin)
os << *begin << " ";
return os;
}
int main()
{
vector<string> things({
"car", "truck", "rabbit"
});
ostringstream oss;
oss << things << endl;
copy(oss.str().begin(), oss.str().end(), ostream_iterator<char>(cout, ""));
}
Run Code Online (Sandbox Code Playgroud)
现在,它与工作cout << things和string str = oss.str(); copy(str.begin() ...,但不与oss.str().begin().据我所知, …
#include <iostream>
#include <string>
#include <sstream>
#include <string>
int coins = 0;
std::stringstream ss;
ss << 100 << ' ' << 200;
Run Code Online (Sandbox Code Playgroud)
当我悬停在上面时,ss我得到错误" 声明没有存储类或类型说明符 ",当我将鼠标悬停在上面时,<<我得到错误预期;".
stringstream ×10
c++ ×9
stl ×2
c++-cli ×1
fmt ×1
lifetime ×1
precision ×1
string ×1
visual-c++ ×1