vector<string> with insert cmd


 
Thread Tools Search this Thread
Top Forums Programming vector<string> with insert cmd
# 1  
Old 09-10-2004
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;
}

# 2  
Old 09-10-2004
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 09:38 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

2. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

3. Shell Programming and Scripting

Need to insert a comment string

I need to insert a comment string to a couple of fields for every line in the file. Different comment string for each field. In below file, fields 7 and 8 "R","1","t0000684","","10/31/2013","10/31/2013","","","","750.23"... (6 Replies)
Discussion started by: Melah Gindi
6 Replies

4. Shell Programming and Scripting

How to paste string yanked into an edit in cmd mode?

I have 2 files open in Vim at the same time. I yank from one with 16yw and get this INSERT INTO ALIS_PART_MANU_PART_TAB (SEQUENCE_NO,PART_NO,MANU_NO,MANU_PART_NO,ROWVERSION) VALUES ( Now I go to the second file and type the command to add this to the beginning of every... (3 Replies)
Discussion started by: djehresmann
3 Replies

5. Shell Programming and Scripting

Insert string in alternate lines

Hi All, In continuation of my previous thread 'Add text at the end of line conditionally', I need to further modfiy the file after adding text at the end of the line. Now, I need to add a fixed charater string at alternate lines starting from first line using awk or sed.My file is now as below:... (10 Replies)
Discussion started by: angshuman
10 Replies

6. Programming

I need to scan my 'vector<string> mystrings' for duplicate entries.

Basically I'm trying to avoid writing paragraphs to do it. It is possible I could convert over to a vector<int>'s if that may help. Does anyone know how to do this without really turning it into a huge brainbuster? I have considered using a for() loop but I wasn't able to put anything into practice... (1 Reply)
Discussion started by: sepoto
1 Replies

7. Shell Programming and Scripting

Unix cmd prompt how to get old cmd run?

Hi, I am using SunOS I want to serch my previous command from unix prompt (like on AIX we can search by ESC -k) how to get in SunOs urgent help require. (10 Replies)
Discussion started by: RahulJoshi
10 Replies

8. Programming

Shared Memory and String,Vector

How can i user string or vector ins shared memory ? For example i have a structure sharedInfo like below struct sharedInfo { string szName; int iAge; string szAddrees; }; if i use this notation my program crashes. And if i use char szName, it work fine, what is wrong with... (4 Replies)
Discussion started by: helpmeforlinux
4 Replies

9. UNIX for Dummies Questions & Answers

cmd sequence to find & cut out a specific string

A developer of mine has this requirement - I couldn't tell her quickly how to do it with UNIX commands or a quick script so she's writing a quick program to do it - but that got my curiousity up and thought I'd ask here for advice. In a text file, there are some records (about half of them)... (4 Replies)
Discussion started by: LisaS
4 Replies

10. Shell Programming and Scripting

fetch string and insert later

"are you billy_smith ?" replace with "are you billy_smith ? my name is billy_smith" how to fetch the name "billy_smith" and using it later I need sed script to do this, any one can help? Thanks (6 Replies)
Discussion started by: playinmel.com
6 Replies
Login or Register to Ask a Question