2009-12-06, 12:47
  #1
Medlem
Kod:
 20 /*Split a string into tokenz and returns all of them */
 22 vector<float> Parser::getRow(string in){
 23     cout << in << endl;
 24     vector<float> row;
 25     string token;
 26     istringstream iss(in);
 27     while ( getline(iss, token, ' ') ){
 28             cout << "here is token that is not added " << token << endl;
 29             if(token.length() > 0){
 30                 cout << "adding" << token << endl;
 31                 row.push_back(atof(token.c_str()));
 32             }
 33     }
 34     cout << "returning row " << endl;
 35     for(int i = 0 ; i < row.size(); i++)
 36         cout << row[i] << ",";
 37     
 38     cout << endl;
 39     return row;
 40 }


I vissa fall fungerar den detta ger den output jag vill ha.

[PHP]192 std::string foo = "3 4 5 6 7";
193 p.getRow(foo);
[/PHP]

När jag kör den i ett större samanhang
Så skriver jag ut in(första jag gör i funktionen)

3 20 19 1

Däremot får jag bara första elementet tillbaka.
returning row
3,

Varför?
Citera
2009-12-06, 12:55
  #2
Medlem
Citat:
Ursprungligen postat av isato
Kod:
 20 /*Split a string into tokenz and returns all of them */
 22 vector<float> Parser::getRow(string in){
 23     cout << in << endl;
 24     vector<float> row;
 25     string token;
 26     istringstream iss(in);
 27     while ( getline(iss, token, ' ') ){
 28             cout << "here is token that is not added " << token << endl;
 29             if(token.length() > 0){
 30                 cout << "adding" << token << endl;
 31                 row.push_back(atof(token.c_str()));
 32             }
 33     }
 34     cout << "returning row " << endl;
 35     for(int i = 0 ; i < row.size(); i++)
 36         cout << row[i] << ",";
 37     
 38     cout << endl;
 39     return row;
 40 }


I vissa fall fungerar den detta ger den output jag vill ha.

[PHP]192 std::string foo = "3 4 5 6 7";
193 p.getRow(foo);
[/PHP]

När jag kör den i ett större samanhang
Så skriver jag ut in(första jag gör i funktionen)

3 20 19 1

Däremot får jag bara första elementet tillbaka.
returning row
3,

Varför?

Den verkar missa att den skall splitta vid ' '
eftersom
cout << "here is token that is not added " << token << endl;

ger here is token that is not added 3 20 21 0

i det fungerande fallet ser det ut såhär
here is token that is not added 3
adding3
here is token that is not added
here is token that is not added 4
adding4
here is token that is not added
here is token that is not added
here is token that is not added 5
adding5
here is token that is not added
here is token that is not added 6
adding6
here is token that is not added
here is token that is not added 7
Citera
2009-12-06, 13:29
  #3
Medlem
Downspouts avatar
Har du verkligen mellanslag mellan talen i den felande strängen? Om man kopierar den ifrån ditt inlägg verkar talen vara separerade med tabbar.

EDIT: Kan ju tillägga att din kod fungerar för mig om jag byter ut tabbarna mot mellanslag.
Citera
2009-12-06, 13:33
  #4
Medlem
Citat:
Ursprungligen postat av Downspout
Har du verkligen mellanslag mellan talen i den felande strängen? Om man kopierar den ifrån ditt inlägg verkar talen vara separerade med tabbar.

EDIT: Kan ju tillägga att din kod fungerar för mig om jag byter ut tabbarna mot mellanslag.

Aha ...så kan det vara. Men jag läser input från en fil så dom är troligen tabbade nu när du säger det. Hur tar jag bort tabbarna? alltså hur skriver jag "tab"

getline(iss, token, 'tab') ?
Citera
2009-12-06, 13:40
  #5
Medlem
Downspouts avatar
'\t' är tab.
Citera
2009-12-06, 14:10
  #6
Medlem
Citat:
Ursprungligen postat av Downspout
'\t' är tab.
jobbigt vissa filer använder mellanslag och andra tab.
Citera
2009-12-06, 14:15
  #7
Medlem
metapods avatar
Citat:
Ursprungligen postat av isato
jobbigt vissa filer använder mellanslag och andra tab.
Then skip the use of std::getline and use the operator>> overload for std::istream and int, that will "ignore" all whitespaces (ie. "\v\f\r\n\t ") and this might not be what you are looking for.
Though there are numerous ways of splitting a string only on tab and space.
Citera
2009-12-06, 14:17
  #8
Medlem
Citat:
Ursprungligen postat av metapod
Then skip the use of std::getline and use the operator>> overload for std::istream and int, that will "ignore" all whitespaces (ie. "\v\f\r\n\t ") and this might not be what you are looking for.
Though there are numerous ways of splitting a string only on tab and space.
Example please?

I found one?
Kod:
 19 vector<float> Parser::getRow(string in){
 20     string str("Split me by whitespaces");
 21     string buf; // Have a buffer string
 22     stringstream ss(str); // Insert the string into a stream
 23 
 24     vector<string> tokens; // Create vector to hold our words
 25     
 26     while (ss >> buf)
 27         tokens.push_back(buf);
 28     
 29 }  
__________________
Senast redigerad av isato 2009-12-06 kl. 14:29.
Citera
2009-12-06, 14:47
  #9
Medlem
Slutresultat:

Kod:
 20 vector<float> Parser::getRow(string str){
 21     vector<float> row;
 22     string buf; // Have a buffer string
 23     stringstream ss(str); // Insert the string into a stream
 24 
 25     while (ss >> buf){
 26         row.push_back(atof(buf.c_str()));
 27     }
 28     return row;
 29 }
Citera
2009-12-06, 19:26
  #10
Medlem
metapods avatar
example 1
Kod:
void
example_1 ()
{
  std::istringstream iss ("1\t2 3\n4 5");
  std::vector<int> v;
  int n;

  while (iss >> n)
    v.push_back (n);
}

example 2
Kod:
struct FB_Facet : std::ctype<char> {
  FB_Facet () : std::ctype<char> (get_mask ()) {}

  private:
    FB_Facet (int) {}

    static std::ctype_base::mask * get_mask () {
      static std::ctype_base::mask m[std::ctype<char>::table_size];
      static bool ready =0;

      if (ready) {
        return m;
      }

      std::copy (
        classic_table (),
        classic_table () + std::ctype<char>::table_size,
        m
      );

      for (std::size_t i =0; i < std::ctype<char>::table_size; ++i) {
        switch (static_cast<unsigned char> (i)) {
          case ' ': case '\t':
            m[static_cast<unsigned char> (i)] |= std::ctype_base::space; break;
          default:
            m[i] &= ~ (std::ctype_base::space);
        }   
      }   

      ready =1; 
      return m;
    }   
};


void
example_2 ()
{
  std::istringstream iss ("1\t2 3\n4 5");

  std::locale loc (std::locale::classic (), new FB_Facet);

  iss.imbue (loc);

  std::vector<std::string> v;
  std::string s1;

  while (iss >> s1)
    v.push_back (s1);
}

example 3

Kod:
void
example_3 ()
{
  std::istringstream iss ("1\t2 3\n4 5");
  std::vector<std::string> v;
  std::string s1;

  while (std::getline (iss, s1, ' ')) {
    std::istringstream iss (s1);

    while (std::getline (iss, s1, '\t'))
      v.push_back (s1);
  }
}
Citera
2009-12-06, 22:39
  #11
Medlem
Thank you very much!
Citera

Skapa ett konto eller logga in för att kommentera

Du måste vara medlem för att kunna kommentera

Skapa ett konto

Det är enkelt att registrera ett nytt konto

Bli medlem

Logga in

Har du redan ett konto? Logga in här

Logga in