我有一个看起来像这样的数据:
AAA 0.3 1.00 foo chr1,100
AAC 0.1 2.00 bar chr2,33
AAT 3.3 2.11 chr3,45
AAG 1.3 3.11 qux chr1,88
ACA 2.3 1.33 chr8,13
ACT 2.3 7.00 bux chr5,122
Run Code Online (Sandbox Code Playgroud)
请注意,上面的行是制表符分隔的.而且,它有时可能包含5个字段或4个字段.
我想要做的是将变量中的第4个字段捕获为"",如果它不包含任何值.
我有以下代码,但它以某种方式读取第5个字段,当第4个为空时为第4个字段.
这样做的正确方法是什么?
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
int main ( int arg_count, char *arg_vec[] ) {
string line;
ifstream myfile (arg_vec[1]);
if (myfile.is_open())
{
while (getline(myfile,line) )
{
stringstream ss(line);
string Tag;
double Val1;
double Val2;
double Field4;
double Field5;
ss >> Tag …Run Code Online (Sandbox Code Playgroud) 我有两个空格分隔的字符串...(X不代表相同的符号)
st1 = "abc def kok...."
st2 = "kok bbr def ffe ...."
Run Code Online (Sandbox Code Playgroud)
我想构建一个交集字符串,如下所示:
common = "kok def"
在c ++中这样做的有效方法是什么?
谢谢
我有一个静态方法,应返回下一个可用的ID.其他类中的几个静态成员调用此函数,因此每个成员都会分配不同的id.但是,当我打印指定的值时,我不会得到"id0","id1",而只是像"*"这样的符号.我正在使用此代码:
int A::i = 0; //static member
std::string A::id()
{
std::stringstream sst;
sst<< "id" << A::i;
i++;
return sst.str(); //i've tried to return a const char* too, but doesnt work either
}
//assigning id's in several classes like this:
const char* B::id = A::id().c_str();
const char* C::id = A::id().c_str();
//printing them in the main function:
std::cout << B::id << " " << C::id << "\n";
Run Code Online (Sandbox Code Playgroud)
我不明白为什么上面的代码不起作用.当我执行以下代码时,打印预期结果"id10":
static std::string gt()
{
std::stringstream ss;
ss << "id" << 10;
return ss.str();
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码,我发现将字符串切成单词.我无法弄清楚while部分是如何工作的.如何知道将没有空格的单词提取到buf变量?似乎提取运算符(>>)既用于将位进入缓冲区,又用于循环返回true - 我只是无法弄清楚它是如何通过空格来切割单词的.
string buf; // Have a buffer string
stringstream ss(str); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.push_back(buf);
Run Code Online (Sandbox Code Playgroud) 几天来我一直试图解决这个问题,但我无法解决。基本上我的代码应该读取由 wmic 生成的 .csv 文件并将其保存到结构中。我可以读取数据并且正在存储它,但是数据在每个字符后都有一个额外的空格。我曾尝试切换到函数的 Unicode 版本并使用宽字符串,但它们只会使数据更加混乱(它们将“n”变成了“ÿ”)。
这是我认为是问题的代码:
system("wmic product get name,version,installdate,vendor /format:csv > product.txt");
std::ifstream infoFile("./program.txt"); // The file wmic wrote in csv format.
if(infoFile.is_open())
{
std::string line;
int lineNum = 0;
while(getline(infoFile, line))
{
lineNum++;
std::cout << "\nLine #" << lineNum << ":" << std::endl;
Program temp;
std::istringstream lineStream(line);
std::string cell;
int counter = 0;
int cellNum = 0;
while(getline(linestream, cell, ','))
{
cellNum++;
std::cout << "\nCell #" << cellNum << ":" << cell << std::endl;
switch(counter)
{ …Run Code Online (Sandbox Code Playgroud) 例如:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int num = 10;
string str;
stringstream toString;
toString << num;
toString >> str;
cout << str << "\n"; //10
int num2 = 20;
string str2;
toString << num2;
toString >> str2;
cout << str2 << "\n"; //str2 is empty
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道我必须清除这个:
toString.str("");
toString.clear();
Run Code Online (Sandbox Code Playgroud)
但是为什么在使用运算符后它不会自动清除>>?
我试图将sprintf语句转换为C++流语句.我试图复制的sprintf格式化语句是"%5.3f"
我正在使用命名空间std并包含和
我有:
double my_double = GetMyDoubleFromSomewhere();
stringstream ss;
ss << ??? << my_double;
Run Code Online (Sandbox Code Playgroud)
我一直在看固定和setprecision,但不能完全弄清楚如何设置原始格式说明符的5和3?
有人可以帮我理解这个串流行为吗?
stringstream temp;
temp << "342 1 ";
int a;
while (temp >> a) {
cout << a << endl;
}
temp << "56" << " ";
temp >> a;
cout << a << endl;
Run Code Online (Sandbox Code Playgroud)
哪个输出:
342
1
1
Run Code Online (Sandbox Code Playgroud)
我希望它输出
342
1
56
Run Code Online (Sandbox Code Playgroud)
这是在视觉工作室2015年编制的.
我想使用自定义分配器从空闲列表中为std::basic_ostringstream. 这是我要使用的自定义分配器:
template <class Tp>
struct NAlloc {
typedef Tp value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
NAlloc() = default;
template <class T> NAlloc(const NAlloc<T>&) {}
Tp* allocate(std::size_t n) {
n *= sizeof(Tp);
memoryPool *memPool = memoryPool::GetInstance(10);//get memory pool instance
std::cout << "allocating " << n << " bytes\n";
return static_cast<Tp*>(memPool->allocate(n)); //get memory from pool
}
void deallocate(Tp* p, std::size_t n) {
std::cout << …Run Code Online (Sandbox Code Playgroud) #include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<int> parseInts(string str) {
istringstream ss(str);
vector<int> integ;
int val;
while(ss){
if(ss>>val){
integ.push_back(val);
}
}
return integ;
}
vector<int> parseInts2(string str)
{
vector<int> vec;
stringstream ss(str);
char ch;
int temp;
while(ss)
{
ss>>temp>>ch; >> operator
vec.push_back(temp);
}
return vec;
}
int main() {
string str;
cin >> str;
vector<int> integers = parseInts(str);
for(int i = 0; i < integers.size(); i++) {
cout << integers[i] << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个流,到一个字符串,从字符串中读取整数到流中并将其插入向量中并显示其元素,而输出不显示任何内容。代码有什么问题? …
stringstream ×10
c++ ×9
string ×2
allocator ×1
c++11 ×1
csv ×1
getline ×1
parsing ×1
return-value ×1