![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
||||
|
||||
|
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;
}
|
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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;
}
seems easier with numbers. Last edited by photon; 09-12-2004 at 05:38 AM. |
||||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|