Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If those are variable numbers of "regions" and variable numbers of file name strings you could make your life easier by using vectors instead of arrays.</p> <p>And in general, I wouldn't do something like this in C++, but rather Python, Ruby, etc. C++ is really not made for such tasks... Anyway, here is some quick and dirty code that should do the job. You can clean it up by refactoring...</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;fstream&gt; #include &lt;sstream&gt; #include &lt;vector&gt; #include &lt;numeric&gt; #include &lt;algorithm&gt; using std::vector; using std::cout; using std::cin; using std::endl; using std::string; using std::ifstream; using std::istringstream; void split(const string &amp;s, vector&lt;string&gt; &amp;elems, char sep=' ') { string element = ""; vector&lt;string&gt; splitted_ints; for (auto c : s) { if (c != sep) element.push_back(c); else { elems.push_back(element); element = ""; } // end if } // end for loop if (element != "") elems.push_back(element); } // end split() int main() { string line; vector&lt;string&gt; first_line; vector&lt;string&gt; second_line; vector&lt;string&gt; splitted_ints; vector&lt;int&gt; from; vector&lt;int&gt; to; vector&lt;string&gt; filenames; ifstream in_file("input.txt"); getline(in_file, line); split(line, first_line, ' '); getline(in_file, line); split(line, second_line, ' '); for (string ele : first_line) split(ele, splitted_ints, ':'); // now you have the integer sequence of the first line // in splitted_ints, e.g., 48, 49, 68, 70, 93, 100 size_t i = 0; while (i &lt; splitted_ints.size()){ int num; istringstream (splitted_ints[i]) &gt;&gt; num; from.push_back(num); ++i; istringstream (splitted_ints[i]) &gt;&gt; num; to.push_back(num); ++i; } splitted_ints.clear(); // repeat for second line for (string ele : second_line) split(ele, splitted_ints, ':'); i = 0; while (i &lt; splitted_ints.size()){ int num; istringstream (splitted_ints[i]) &gt;&gt; num; from.push_back(num); ++i; istringstream (splitted_ints[i]) &gt;&gt; num; to.push_back(num); ++i; } // append the rest of the input file (i.e., the filenames) while (getline(in_file, line)) { filenames.push_back(line); } </code></pre> <p>// Printing out the contents to make sure it worked</p> <pre><code> cout &lt;&lt; "FILENAMES:\n"; for (auto ele : filenames) cout &lt;&lt; ele &lt;&lt; ","; cout &lt;&lt; "\nFROM numbers:\n"; for (auto ele : from) cout &lt;&lt; ele &lt;&lt; ","; cout &lt;&lt; "\nTO numbers:\n"; for (auto ele : to) cout &lt;&lt; ele &lt;&lt; ","; cout &lt;&lt; endl; /* FILENAMES: tr429a.frank/tr429a.reg1.0.pdb ,tr429a.frank/tr429a.reg1.1.pdb ,tr429a.frank/tr429a.reg1.2.pdb ,tr429a.frank/tr429a.reg1.3.pdb ,tr429a.frank/tr429a.reg1.4.pdb ,tr429a.frank/tr429a.reg1.5.pdb ,tr429a.frank/tr429a.reg1.6.pdb ,tr429a.frank/tr429a.reg1.7.pdb, FROM numbers: 48,68,93,22,34,50,71, TO numbers: 49,70,100,33,47,67,92, */ return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload