我正在模拟一些 Ising 模型(带有 1 或 -1 的格子),我不想使用文件(我想这里不打算详细介绍:D)。所以,问题是,我使用字符串流来格式化数据和数据的文件名,然后用 bash 将它们分开。显示示例代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void print2bash(string file, string data);
int main(int argc, char *argv[]){
int i, j, n = atoi(argv[1]);
// I want to put this outside main() {
stringstream ssbash; ssbash.clear(); ssbash.str("");
stringstream ssfile; ssfile.clear(); ssfile.str("");
//}
// So I don't have to do ss.clear and ss.str("") every time before using them
ssfile << "this_" << n << "_is_the_filename.dat" << endl;
for(i = 0; i < n ; i++){
for(j = 0; j < n; j++)
ssbash << i*n+j << "\t";
ssbash << endl;
}
print2bash( ssfile.str(), ssbash.str() );
return 0;
}
// print2bash(); prints string data; to string file;
// This will be used to separate data
// afterwards ( i.e. ./Ising.o <arguments> | bash )
void print2bash(string file, string data){
stringstream ssbash; ssbash.clear(); ssbash.str(data);
stringstream output; output.clear(); output.str("");
string line;
output << "for((i=0;i<1;i++)) do "<< endl; // Buffer all to bash at once
while( getline(ssbash,line) )
output << " echo \"" << line << "\";" << endl;
output << "done >> " << file; // Appending
cout << output.str() << endl;
ssbash.clear(); ssbash.str("");
return ;
}
这个“sstreams.o”程序的常规输出是
~work/Ising$ ./sstreams.o 5
for((i=0;i<1;i++)) do
echo "0 1 2 3 4 ";
echo "5 6 7 8 9 ";
echo "10 11 12 13 14 ";
echo "15 16 17 18 19 ";
echo "20 21 22 23 24 ";
done >> this_5_is_the_filename.dat
你明白吗?从这个思维流程来看,我实际上写的是./sstreams.o 10 | bash
这样一个很好的分离文件。但问题是:
每次我想使用 print2bash() 时,我都厌倦了写这些行(哦,具有讽刺意味!)
stringstream ssbash; ssbash.clear(); ssbash.str("");
stringstream ssfile; ssfile.clear(); ssfile.str("");
正因为如此,我想把ss
as 全局变量(不知道这是否是那种可怕的,所以编译器不让我这样做:我只是一个物理学家)。因此,即使我在s (和)std::stringstream
之后为 ss 变量编写,我仍然会收到错误#include
using namespace std
error: ‘ssbash’ does not name a type
error: ‘ssbash’ does not name a type
error: ‘ssfile’ does not name a type
error: ‘ssfile’ does not name a type
从编译器。希望你能帮助我。
编辑:[已解决]
问题是,如果我想声明一个stringstream
out of main()
it 失败,因为成员clear
& str
where 脱离上下文,所以它们不能被使用。stringstream
除此之外,正如@jpalecek 和@umlaeute 指出的那样,这种使用是不必要的,因为s 是用空内容创建的。
我已经将stringstream
s 声明为 out of main 并包含一个clear()
+str("")
以print2bash()
提供所需的结果:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
stringstream ssbash, ssfile;
void print2bash(string file, string data);
int main(int argc, char *argv[]){
int i, j, n = atoi(argv[1]), p, np = atoi(argv[2]);
vector< vector<int> > chain; chain.resize(n*n);
for( p = 0; p < np; p++){
ssfile << "chain_p="<< p << "_filename.dat" << endl;
for(i = 0; i < n ; i++){
for(j = 0; j < n; j++){
chain[p].push_back( i*n + j );
ssbash << i*n+j << "\t";
}
ssbash << endl;
}
print2bash( ssfile.str(), ssbash.str() );
}
return 0;
}
void print2bash(string file, string data){
ssbash.str(data);
stringstream output;
string line;
output << "for((i=0;i<1;i++)) do "<< endl; // Buffer all to bash at once
while( getline(ssbash,line) )
output << " echo \"" << line << "\";" << endl;
output << "done >> " << file; // Appending
cout << output.str() << endl;
ssfile.clear(); ssfile.str("");
ssbash.clear(); ssbash.str("");
return ;
}
的常规输出./sstreams.o
~/work/Ising$ ./sstreams.o 4 2
for((i=0;i<1;i++)) do
echo "0 1 2 3 ";
echo "4 5 6 7 ";
echo "8 9 10 11 ";
echo "12 13 14 15 ";
done >> chain_p=0_filename.dat
for((i=0;i<1;i++)) do
echo "0 1 2 3 ";
echo "4 5 6 7 ";
echo "8 9 10 11 ";
echo "12 13 14 15 ";
done >> chain_p=1_filename.dat
感谢一切