逐行重复数字由下一个数字逗号分隔显示的次数
Repeat numbers one by line the amount of times shown by the next number coma separated
我必须使用来自 CSV 的数据创建一个 TXT 文件。
它们目前的排序格式为 1,3,2,4,7,2,3,1,4,3 。当 1 是要重复的次数,下一个数字是要重复的次数时。
objective 的输出格式为:
1
1
1
2
2
2
2
7
7
3
4
4
4
我目前只能显示一个在另一个下面的数字,但不能显示重复的数字。任何帮助将不胜感激。这是我第一次post来这里,所以如果有任何问题请告诉我,谢谢!
void lectura(string archivo){
string linea; //string to save the line of numbers
vector <string> numeros; //vector to save the line
ifstream entrada(archivo.c_str()); //open csv to read
ofstream signal( "datos_de_senial.txt", ios::out); //open csv to write
int pos =0;
while(getline(entrada, linea)){ //get the line
istringstream in(linea); //convert to istingstream
string num;
if(pos==0){
while (getline(in, num, ',')){ //get the numbers separated by ","
numeros.push_back(num); //save to vector"numeros"
}
for(unsigned int x = 0; x < numeros.size(); x++) //show one number below the other,here i think the problem is
signal << numeros[x] << '\n';
}
}
signal.close();
}
int main(int argc, char *argv[]) {
void lectura(string archivo);
string csv = "signals.csv";
lectura(csv);
return 0;
}
我为您起草了一个非常简单的解决方案。这样,您就可以很容易地看到如何根据需要打印值。
由于此解决方案非常简单,因此无需进一步解释。
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
#include <regex>
std::regex re(",");
std::string csv{"1,3,2,4,7,2,3,1,4,3"};
int main() {
// Read all data from the file
std::vector<std::string> data(std::sregex_token_iterator(csv.begin(),csv.end(),re,-1), {});
// iterate through data
for (std::vector<std::string>::iterator iter = data.begin(); iter != data.end(); ++iter) {
// Read the value
int value = std::stoi(*iter);
// Point to next value, the repeat count
++iter;
int repeatCount = std::stoi(*iter);
// Now output
for (int i=0; i < repeatCount; ++i)
std::cout << value << "\n";
}
return 0;
}
当然还有很多其他可能的解决方案。 . .
我必须使用来自 CSV 的数据创建一个 TXT 文件。
它们目前的排序格式为 1,3,2,4,7,2,3,1,4,3 。当 1 是要重复的次数,下一个数字是要重复的次数时。
objective 的输出格式为:
1
1
1
2
2
2
2
7
7
3
4
4
4
我目前只能显示一个在另一个下面的数字,但不能显示重复的数字。任何帮助将不胜感激。这是我第一次post来这里,所以如果有任何问题请告诉我,谢谢!
void lectura(string archivo){
string linea; //string to save the line of numbers
vector <string> numeros; //vector to save the line
ifstream entrada(archivo.c_str()); //open csv to read
ofstream signal( "datos_de_senial.txt", ios::out); //open csv to write
int pos =0;
while(getline(entrada, linea)){ //get the line
istringstream in(linea); //convert to istingstream
string num;
if(pos==0){
while (getline(in, num, ',')){ //get the numbers separated by ","
numeros.push_back(num); //save to vector"numeros"
}
for(unsigned int x = 0; x < numeros.size(); x++) //show one number below the other,here i think the problem is
signal << numeros[x] << '\n';
}
}
signal.close();
}
int main(int argc, char *argv[]) {
void lectura(string archivo);
string csv = "signals.csv";
lectura(csv);
return 0;
}
我为您起草了一个非常简单的解决方案。这样,您就可以很容易地看到如何根据需要打印值。
由于此解决方案非常简单,因此无需进一步解释。
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
#include <regex>
std::regex re(",");
std::string csv{"1,3,2,4,7,2,3,1,4,3"};
int main() {
// Read all data from the file
std::vector<std::string> data(std::sregex_token_iterator(csv.begin(),csv.end(),re,-1), {});
// iterate through data
for (std::vector<std::string>::iterator iter = data.begin(); iter != data.end(); ++iter) {
// Read the value
int value = std::stoi(*iter);
// Point to next value, the repeat count
++iter;
int repeatCount = std::stoi(*iter);
// Now output
for (int i=0; i < repeatCount; ++i)
std::cout << value << "\n";
}
return 0;
}
当然还有很多其他可能的解决方案。 . .