Prikaz jedne poruke
Stara 26.1.2012, 4:44   #1
Ivan-94
Veteran
 
Član od: 15.3.2009.
Lokacija: Beograd
Poruke: 654
Zahvalnice: 240
Zahvaljeno 63 puta na 43 poruka
Slanje poruke preko MSN-a korisniku Ivan-94 Slanje poruke preko Skypea korisniku Ivan-94
Određen forumom How to... C++

Kako krenem da ucim jezik, tako pocinju teme How to....

Kod:
Spoiler za Kod:
Kod:
// O-recnik.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

vector<string> split(const string& s, const string& delim, const bool keep_empty = true) {
    vector<string> result;
    if (delim.empty()) {
        result.push_back(s);
        return result;
    }
    string::const_iterator substart = s.begin(), subend;
    while (true) {
        subend = search(substart, s.end(), delim.begin(), delim.end());
        string temp(substart, subend);
        if (keep_empty || !temp.empty()) {
            result.push_back(temp);
        }
        if (subend == s.end()) {
            break;
        }
        substart = subend + delim.size();
    }
    return result;
}

string toLower(string strr)
{	
	char str[100];
	string ret;
	strcpy(str,strr.c_str());
	int differ = 'A'-'a';
	char ch;
	int ii = strlen(str);
	for (int i=0; i <ii;i++)                                                           
	{
		strncpy(&ch,str+i,1);
		if (ch>='A' && ch<='Z')
		{
			ch = ch-differ;
			memcpy(str+i,&ch,1);
		}
	}
	ret = str;
	return ret;
}

int _tmain(int argc, _TCHAR* argv[])
{
	vector<string> words;
	int n;
	string temp;
	bool flag = true;
	bool nflag = true;
	int counter = 0;

	cin >> n;

	for(int i = 0; i < n; i++){
		cin >> temp;
		vector<string> x = split(temp, " ");
		flag = true;
		counter = 0;

		if(toLower(x[0]) == "add"){
			 for (int i = 0; i < words.size(); i++)
				 if(find(words.begin(), words.end(), x[1]) != words.end())
					 flag = false;

			 if(flag){
				 words.push_back(toLower(x[1]));
				 sort( words.begin(), words.end() );
			 }
		}

		if(toLower(x[0]) == "less"){
			if(find(words.begin(), words.end(), x[1]) == words.end())
				cout << "no such word" << endl;
			else {
				for (int i = 0; i < words.size(); i++)
				if(words[i] != toLower(x[1]))
					 counter++;
				 else
					break;
				cout << counter << endl;
			}
		}
	}

	return 0;
}


I izlazi mi stalno problem "vector subscript out of range", a ja nemam pojma kako to da popravm.Video sam da ima neke veze sa indeksima elemenata, ali ja ne vidim problem.
Ivan-94 je offline   Odgovor sa citatom ove poruke