The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Vector Traversing dhanamurthy High Level Programming 1 04-30-2008 05:20 AM
Can I use sed to insert a string which has colon Jenny.palmy UNIX for Dummies Questions & Answers 2 04-28-2008 04:04 PM
How to insert a string at the end of a file read ahjiefreak Shell Programming and Scripting 5 12-10-2007 08:38 PM
how to insert line break + string in vi (search & replace ) umen Shell Programming and Scripting 1 06-08-2006 08:42 AM
Failed to insert string into file before line nir_s Shell Programming and Scripting 7 07-25-2005 10:37 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-10-2004
photon's Avatar
Registered User
 
Join Date: Jul 2002
Posts: 148
vector<string> with insert cmd

How do I correct this vector<string> insert.

I am geeting segmintation dump.


Code:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
//#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;



struct str{
  int go(vector<string> s){
    vector<string>::iterator vsitr0;
    vector<string>::iterator vsitr1;
    vector<string> vs;
    int ret = 0;
    int count = 0;
    for(vsitr0 = s.begin(); vsitr0 != s.end() - 1; ++vsitr0){
      count = 0;
      for(vsitr1 = s.end() - 1; vsitr1 != vsitr0; --vsitr1){
        if(*vsitr0 > *vsitr1){
          count++;
          // HERE IT IS/////
          s.insert(vsitr1, *vsitr0);
          ///////////////////////////////
        }
      }
      if(count > 0) ret++;
    }
    return ret;
  }
	
//////	
};

int main() {
  vector<string> s;
  s.push_back("Aaa");
  s.push_back("Ppp");
  s.push_back("C is cool");
  s.push_back("Apple");
  str m;
  int ans = 0;
  ans = m.go(s);
  cout << ans << endl;
}
Reply With Quote
Forum Sponsor
  #2  
Old 09-10-2004
photon's Avatar
Registered User
 
Join Date: Jul 2002
Posts: 148
Needs an erase or it goes infinite.

Solved this way.

Code:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
//#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;



struct str{
  int go(vector<string> vec){
        
    int ret = 0;
    int count = 0;
    for(int i = 0 ; i < vec.size() - 1; i++){
	count = 0;
	for(int j = vec.size() - 1; j > i ; j-- ){
	  if(vec[i] > vec[j]){
	    count++;
	    vec.insert(vec.begin() + i,vec[j]);
            vec.erase(vec.begin() + (j+1));
	  }
	}
	if(count > 0) ret++;
    }
    return ret;
  }
  
  
//////	
};

int main() {
  vector<string> s;
  s.push_back("Aaa");
  s.push_back("Ppp");
  s.push_back("C is cool");
  s.push_back("Apple");
  str m;
  int ans = 0;
  ans = m.go(s);
  cout << ans << endl;
}
I think I may drop the use of ::iterators, loops
seems easier with numbers.

Last edited by photon; 09-12-2004 at 05:38 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0